Image, saved to sdcard, doesn't appear in Android's Gallery app

AndroidImageSaveGallery

Android Problem Overview


I save an image to the sdcard and it doesn't appear in the Gallery application until I pull off the sdcard and return it back.

Do you have any idea why is it so?

Seems like the Gallery application has some cache that isn't updated on file save...

Actually, I also want to open the just-saved image in Gallery application and have no success with that
this is my question about this issue.

Android Solutions


Solution 1 - Android

A simpler solution is to use the static convenience method scanFile():

File imageFile = ...
MediaScannerConnection.scanFile(this, new String[] { imageFile.getPath() }, new String[] { "image/jpeg" }, null);

where this is your activity (or whatever context), the mime-type is only necessary if you are using non-standard file extensions and the null is for the optional callback (which we don't need for such a simple case).

Solution 2 - Android

My answer to the original question and to anyone else that may have this problem:

I was having this same problem, images in my app that people saved to the SD card were not showing up in their Gallery immediately. After some searching I found this one line of code inserted after my 'save to sdcard' code that fixed the problem:

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

Solution 3 - Android

The system scans the SD card when it is mounted to find any new image (and other) files. If you are programmatically adding a file, then you can use this class:

http://developer.android.com/reference/android/media/MediaScannerConnection.html

Solution 4 - Android

You can also add an Image to the Media Gallery by intent, have a look at the example code to see how it is done:

ContentValues image = new ContentValues();

image.put(Images.Media.TITLE, imageTitle);
image.put(Images.Media.DISPLAY_NAME, imageDisplayName);
image.put(Images.Media.DESCRIPTION, imageDescription);
image.put(Images.Media.DATE_ADDED, dateTaken);
image.put(Images.Media.DATE_TAKEN, dateTaken);
image.put(Images.Media.DATE_MODIFIED, dateTaken);
image.put(Images.Media.MIME_TYPE, "image/png");
image.put(Images.Media.ORIENTATION, 0);

 File parent = imageFile.getParentFile();
 String path = parent.toString().toLowerCase();
 String name = parent.getName().toLowerCase();
 image.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
 image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
 image.put(Images.Media.SIZE, imageFile.length());

 image.put(Images.Media.DATA, imageFile.getAbsolutePath());

 Uri result = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);

Solution 5 - Android

Gallery refresh including Android KITKAT

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
		Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
		File f = new File("file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
		Uri contentUri = Uri.fromFile(f);
		mediaScanIntent.setData(contentUri);
		this.sendBroadcast(mediaScanIntent);
}
else
{
		sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}

Solution 6 - Android

Here is the code for the MediaScannerConnection:

MyMediaConnectorClient client = new MyMediaConnectorClient(newfile);
MediaScannerConnection scanner = new MediaScannerConnection(context, client);
client.setScanner(scanner);
scanner.connect();

newfile is the File object of your new/saved file.

Solution 7 - Android

there is an app in the emulator that says - ' Dev Tools'

click on that and select ' Media Scanning'.. all the images ll get scanned

Solution 8 - Android

Let your activity implement 'MediaScannerConnectionClient' and add this to your activity:

private void startScan() 
{ 
	if(conn!=null) conn.disconnect();  
	conn = new MediaScannerConnection(YourActivity.this,YourActivity.this); 
	conn.connect(); 
} 

@Override 
public void onMediaScannerConnected() { 
	try{
	    conn.scanFile(yourImagePath, "image/*");
	   } catch (java.lang.IllegalStateException e){
	   }
}

@Override 
public void onScanCompleted(String path, Uri uri) { 
	conn.disconnect(); 
} 

Solution 9 - Android

this work with me

File file = ..... // Save file

context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));

Solution 10 - Android

 File folderGIF = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/newgif2");    //path where gif will be stored
success = folderGIF.mkdir();    //make directory
 String finalPath = folderGIF + "/test1.gif";  //path of file
.....
/* changes in gallery app if any changes in done*/
 MediaScannerConnection.scanFile(this,
                    new String[]{finalPath}, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                        public void onScanCompleted(String path, Uri uri) {
                            Log.i("ExternalStorage", "Scanned " + path + ":");
                            Log.i("ExternalStorage", "-> uri=" + uri);
                        }
                    });

Solution 11 - Android

Here I am sharing code that can load image in form of bitmap from and save that image on sdcard gallery in app name folder. You should follow these steps

  1. Download Image Bitmap first

    
    
    
    private Bitmap loadBitmap(String url) {
    try {
    InputStream in = new java.net.URL(url).openStream();
    return BitmapFactory.decodeStream(in);
    } catch (Exception e) {
    e.printStackTrace();
    }
    return null;
    }
    
    
    

  1. Please also provide following permission in your AndroidManifest.xml file.



uses-permission android:name="android.permission.INTERNET" 
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"





  1. Here is whole code that is written in Activty in which we want to perform this task.



 void saveMyImage(String appName, String imageUrl, String imageName) {

		Bitmap bmImg = loadBitmap(imageUrl);
		File filename;
		try {
			String path1 = android.os.Environment.getExternalStorageDirectory()
					.toString();
			File file = new File(path1 + "/" + appName);
			if (!file.exists())
				file.mkdirs();
			filename = new File(file.getAbsolutePath() + "/" + imageName
					+ ".jpg");
			FileOutputStream out = new FileOutputStream(filename);
			bmImg.compress(Bitmap.CompressFormat.JPEG, 90, out);
			out.flush();
			out.close();
			ContentValues image = new ContentValues();
			image.put(Images.Media.TITLE, appName);
			image.put(Images.Media.DISPLAY_NAME, imageName);
			image.put(Images.Media.DESCRIPTION, "App Image");
			image.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
			image.put(Images.Media.MIME_TYPE, "image/jpg");
			image.put(Images.Media.ORIENTATION, 0);
			File parent = filename.getParentFile();
			image.put(Images.ImageColumns.BUCKET_ID, parent.toString()
					.toLowerCase().hashCode());
			image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, parent.getName()
					.toLowerCase());
			image.put(Images.Media.SIZE, filename.length());
			image.put(Images.Media.DATA, filename.getAbsolutePath());
			Uri result = getContentResolver().insert(
					MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
			Toast.makeText(getApplicationContext(),
					"File is Saved in  " + filename, Toast.LENGTH_SHORT).show();
		} catch (Exception e) {
			e.printStackTrace();
		}

	}





  1. Hope that it can solve your whole problem.

Solution 12 - Android

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

Does not seem to work on KITKAT. It throws permission denial exception and crashes the app. So for this, I have done the following,

String path = mediaStorageDir.getPath() + File.separator
					+ "IMG_Some_name.jpg";
CameraActivity.this.sendBroadcast(new Intent(
			                 Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri
					        .parse("file://" + path)));

Hope it helps.

Solution 13 - Android

My code for MyMediaConnectorClient:

public class MyMediaConnectorClient implements MediaScannerConnectionClient {

	String _fisier;
	MediaScannerConnection MEDIA_SCANNER_CONNECTION;
	
	public MyMediaConnectorClient(String nume) {
		_fisier = nume;
	}
	
	public void setScanner(MediaScannerConnection msc){
		MEDIA_SCANNER_CONNECTION = msc;
	}

	@Override
	public void onMediaScannerConnected() {
		MEDIA_SCANNER_CONNECTION.scanFile(_fisier, null);
	}

	@Override
	public void onScanCompleted(String path, Uri uri) {
		if(path.equals(_fisier))
            MEDIA_SCANNER_CONNECTION.disconnect();
	}
}

Solution 14 - Android

Use this after saving the image

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

Solution 15 - Android

You need to give permissions to the Gallery app. Just long press the gallery app icon in the home screen and tap on 'APP INFO' that pops up at the top of the screen. Doing it will show the gallery app settings. Now go in Permissions tab and enable the storage, camera permissions by toggling it. Now go to your native gallery app and you will get the your saved images.

Solution 16 - Android

This will aslo solve your problem if your image in gallery are not showing instead they might showing a 404 type bitmap in midle. please add a the tags that are in my code with your image because there must some meta data in order to show image in gallery.

      String resultPath = getExternalFilesDir(Environment.DIRECTORY_PICTURES)+ 
      getString(R.string.directory) + System.currentTimeMillis() + ".jpg";
     
       new File(resultPath).getParentFile().mkdir();

        try {
            OutputStream fileOutputStream = new FileOutputStream(resultPath);
            savedBitmap.compress(CompressFormat.JPEG, 100, fileOutputStream);
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (IOException e2) {
            e2.printStackTrace();
        }
        savedBitmap.recycle();

        File file = new File(resultPath);
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, "Photo");
        values.put(MediaStore.Images.Media.DESCRIPTION, "Edited");
        values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis ());
        values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis());
        values.put(MediaStore.Images.ImageColumns.BUCKET_ID, file.toString().toLowerCase(Locale.US).hashCode());
        values.put(MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME, file.getName().toLowerCase(Locale.US));
        values.put("_data", resultPath);

        ContentResolver cr = getContentResolver();
        cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);




        return  resultPath;

Solution 17 - Android

Try this one, it will broadcast about a new image created, so your image visible. inside a gallery. photoFile replace with actual file path of the newly created image

private void galleryAddPicBroadCast() {
        Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri contentUri = Uri.fromFile(photoFile);
        mediaScanIntent.setData(contentUri);
        this.sendBroadcast(mediaScanIntent);
    }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMichael KesslerView Question on Stackoverflow
Solution 1 - AndroiddarrenpView Answer on Stackoverflow
Solution 2 - AndroidShadowGodView Answer on Stackoverflow
Solution 3 - AndroidhackbodView Answer on Stackoverflow
Solution 4 - AndroidJanuszView Answer on Stackoverflow
Solution 5 - AndroidAtanuCSEView Answer on Stackoverflow
Solution 6 - Android3dmgView Answer on Stackoverflow
Solution 7 - AndroidAbhishek SusarlaView Answer on Stackoverflow
Solution 8 - AndroidTOMKAView Answer on Stackoverflow
Solution 9 - Androiduser3245604View Answer on Stackoverflow
Solution 10 - AndroidKhyati VaraView Answer on Stackoverflow
Solution 11 - AndroidVISHNU GARGView Answer on Stackoverflow
Solution 12 - AndroidMuhammad RiyazView Answer on Stackoverflow
Solution 13 - AndroidMarianView Answer on Stackoverflow
Solution 14 - AndroidDeepuView Answer on Stackoverflow
Solution 15 - AndroidDevanshu JainView Answer on Stackoverflow
Solution 16 - AndroidMubashir MurtazaView Answer on Stackoverflow
Solution 17 - AndroidAnand SaggamView Answer on Stackoverflow