title: Move Image Taken With Camera Leaves Broken Link Unitl SD Card Remount
body: <p>I have an application in which I can use the device's camera to take a picture. What I would like to do is to start the ACTION_IMAGE_CAPTURE intent without assigning an EXTRA_OUTPUT, and then move the file that is created in the default location to my own custom location using file.renameTo. My code is something like this:</p>
  
  <pre><code>/* Start camera activity without EXTRA_OUTPUT */
  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  startActivityForResult(intent, _REQUESTCODE_ATTACH_CAMERA);
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (resultCode == RESULT_OK) {
          switch(requestCode) {
              case _REQUESTCODE_ATTACH_CAMERA:
                  /* Get path to most recently added image */
                  final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA };
                  final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
                  Cursor imageCursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy);
                  String fullPath = "";
                  if(imageCursor.moveToFirst()){
                      fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA));
                      imageCursor.close();
                  }
  
                  File f = Environment.getExternalStorageDirectory();
                  f = new File(f.getAbsolutePath() + File.separator + "DCIM" + File.separator + MY_APP_NAME;
                  if(!f.exists()) {
                      f.mkdirs();
                  }
  
                  /* Create new file based on name of most recently created image */
                  File oldFile = new File(fullPath);
                  String newPath = f.getAbsolutePath() + File.separator + oldFile.getName() ;
  
                  /* Move file with renameTo */
                  oldFile.renameTo(new File(newPath));
  
                  break;
              ...
          }
      }
  }
  </code></pre>
  
  <p>All of this works quite well, however there is one strange thing that is occurring. In my app, I have another button that allows selecting an existing image from the phone's gallery. That code looks like this:</p>
  
  <pre><code>Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
  galleryIntent.setType("image/*");
  activity.startActivityForResult(galleryIntent, _REQUESTCODE_ATTACH_GALLERY);
  </code></pre>
  
  <p>This also works, but if I take a picture with the camera using the code posted above, and then try to select another image from the gallery, there will be blank "broken link" type items in the gallery that contain no content and are unselectable. These seem to correspond with photos taken and moved using renameTo; if I put in code in onActivityResult to post the filename to LogCat, the name that gets logged is the same as the name of the previously moved file that it corresponds to. Trying to create a File object or in any way access that filename, results in null objects and force closes.</p>
  
  <p>The strange part is that there is no evidence of these "broken link" files in Eclipse DDMS, nor in the phone itself if I use Root Browser, and they disappear if I remount the SD Card.</p>
  
  <p>The whole reason I am moving the images after capturing them with the camera is to avoid filling up the phone's gallery storage with unnecessary images. While these empty "broken link" type files don't appear to be taking up any storage space, they would still be very annoying to an end-user trying to browse through their gallery. Does anyone have any ideas on what is happening here or how to solve this problem? </p>
  
  <p><strong>EDIT:</strong>
  Here is a photo showing what the gallery looks like with a "broken link" type image displayed. One of these will appear for every photo that is taken using my app, and they will all disappear if I remount the SD Card.
  <img src="https://i.stack.imgur.com/6RzFz.jpg" alt="Screenshot showing &quot;broken link&quot; type image in android gallery"></p>