Photo taken by my app does not appear in Gallery

6

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

Can anyone tell me if I have permission to give the photos so they can be viewed in the gallery? Or even if this would be an Android configuration.

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);    
}
    
asked by Gomes 10.07.2014 в 13:39

1 answer

8

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

In order for the Gallery to be updated immediately you need to call the method scanFile of class MediaScannerConnection .

Set 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 launch 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))));
    
10.07.2014 / 14:18