Question

Photos were saved to Gallery by my application but Gallery does not see this photos. I use file manager to check existing and they are there! I can open them from file manager, but not Gallery.

Code that I use to save picture:

public static void saveImg(long pid, ImageView picture, Context context) {
        File file = new File(Environment.getExternalStorageDirectory() + "/Lackrosy/"  + pid + ".jpeg");
        File dir = new File(Environment.getExternalStorageDirectory() + "/Lackrosy/");
        try {
            if (!dir.exists())
                dir.mkdir();
            picture.buildDrawingCache();
            Bitmap bmap = picture.getDrawingCache();
            FileOutputStream out = new FileOutputStream(file);
            bmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            Log.d("palval", "Photo saved: " + file.getAbsolutePath());
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("palval", e.toString());
            file.delete();
            Toast.makeText(context, "Image not saved", Toast.LENGTH_SHORT).show();
        }
    }
Was it helpful?

Solution

You need to send a broadcast so the gallery updates it self.

        Intent mediaScanIntent = new Intent("android.intent.action.MEDIA_SCANNER_SCAN_FILE");
        File f = new File(image_file_path);
        Uri contentUri = Uri.fromFile(f);
        mediaScanIntent.setData(contentUri);
        sendBroadcast(mediaScanIntent);

Edit 1: You have everything here: http://developer.android.com/training/camera/photobasics.html

You can download the example, its the only code i found that worked on all phones with picture taking, storing in my desired location and showing up in the gallery...

BUt some phones will have duplicates in gallery like HTC desire C...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top