Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8861673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:32:35+00:00 2026-06-14T15:32:35+00:00

I have an application in which I can use the device’s camera to take

  • 0

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:

/* 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;
            ...
        }
    }
}

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:

Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
activity.startActivityForResult(galleryIntent, _REQUESTCODE_ATTACH_GALLERY);

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.

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.

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?

EDIT:
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.
Screenshot showing "broken link" type image in android gallery

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:32:36+00:00Added an answer on June 14, 2026 at 3:32 pm

    Thanks in part to this SO thread, I have discovered a solution. It actually makes sense that it would behave this way since there is a table kept for media content and so removing something without telling the table would definitely create a “broken link” type scenario.

    The ultimate solution is to use contentResolver.delete to remove the reference to the file within the content resolver, but there are two different ways that I have found that will work.

    /* Moving with renameTo */
    //Use the same exact code as I had before (shortened for brevity) to move the file
    oldFile.renameTo(newFile);
    //Get URI from contentResolver using file Id from cursor
    Uri oldUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media._ID)));
    //Delete old file
    getContentResolver().delete(oldUri, null, null);
    

    Getting the URI in this way is necessary because it requires a reference to the image in the contentResolver rather than the path to its location in storage. This way might feel dirty to some since you are moving a file and then calling a delete function on that file in order to sort of trick the content resolver into removing the link to the file. If you would rather, you can do it without using renameTo so that the call to delete(…) actually does delete the image.

    /* Moving with streams */
    //Get streams
    InputStream in = new FileInputStream(oldFile);
    OutputStream out = new FileOutputStream(newFile);
    byte[] buffer = new byte[1024];
    int bytesRead = 0;
    //Read old file into new file
    while((bytesRead = in.read(buffer)) > 0) {
        out.write(buffer, 0, bytesRead);
    }
    
    //Get URI from contentResolver using file Id from cursor
    Uri oldUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media._ID)));
    //Delete old file
    getContentResolver().delete(oldUri, null, null);
    

    The call to contentResolver.delete is the same either way, I just wanted to point out that it will still work if the image has already been removed.

    During this I discovered a solution to a problem that I didn’t even realize that I had that I will post here as well in case anyone with this same problem comes across this in the future. In order to keep the image as selectable in the device gallery from the new location, you need to let the media scanner know that a change has been made. There are two ways that I found to do this:

    /* This is the only way that I know of to handle multiple new files at once. I 
       really would use this sparingly, however, since it will rescan the entire 
       SD Card. Not only could this take a long time if the user has a lot of files 
       on their card, it will also show a notification so it is not exactly a 
       transparent operation. */
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
    
    /* You *could* do multiple files with this by passing in the path for each one 
       in the array of Strings, however an instance of this will get called for each 
       one rather than it doing them all at once. Likewise, your onScanCompleted 
       (if you choose to include one) will get called once for each file in the list. 
       So really, while this is much better for a small number of files, if you plan 
       on scanning a very large amount then the full rescan above would probably be 
       a better option. */
    MediaScannerConnection.scanFile(context, new String[]{ newFilePathAsString }, null, 
        new MediaScannerConnection.OnScanCompletedListener() { 
            public void onScanCompleted(String path, Uri uri) {
                //This executes when scanning is completed
            }
        }
    );
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a socket application which I can use in local network, at home.
I have an application which I would like to enable push notifications on. I
I have an application which requires authentication, but has some related services which can
I have a console application which can also open winform under certain conditions.Processing(its a
We have a delphi application which can also run as a sevice . We
I have an application in which you can accept friend request. For this purpose
I have a java web application which I can debug using IntelliJ or Eclipse
I have two async classes in my application which you can see below class
I just want to know that can i make an application which have some
In my application I have an edit profile page which you can see below;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.