Photo taken by my app does not appear in Gallery

Asked

Viewed 407 times

6

When generating the photo through my app it correctly creates the folder and saves the photos taken there, but when I go in the Mobile Gallery is as if the photos did not exist, the default Android gallery does not recognize the files.

Would anyone care to tell me if you have any permission I need to give so that the photos can be viewed in the gallery? Or even if this would be an Android setting.

I’m Using Android 4.0

private void addImage() {    
     Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);    
     intent.setData(uri);           
     this.sendBroadcast(intent);    
}

1 answer

8


This is because the Gallery is only updated, by the Android OS, from time to time.

So that the Gallery be updated immediately you need to call the method scanFile class MediaScannerConnection.

Define the following methods in your program:

void doScanFile(String fileName) {
        String[] filesToScan = {fileName};

        MediaScannerConnection.scanFile(this, filesToScan, null,
                new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String filePath, Uri uri) {
                        mediaFileScanComplete(filePath, uri);
                    }
                });
}

void mediaFileScanComplete(String mediaFilePath, Uri mediaFileUri) {

   //Guarde esta informação se você necessitar dela.
    _lastMediaFilePath = mediaFilePath;
    _lastMediaFileUri = mediaFileUri;
}

Call the method doScanFile(fileName) after saving the photo.

Alternatively you can release a Broadcast to indicate that there has been a change in the media file:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(fileName))));
  • It worked fine, thank you very much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.