Splash
Splash

Reputation: 181

Can't see the bitmap I saved to new directory

I was looking for a way to save a bitmap to a folder (with my app's name), in such a way the default gallery will recognize it.

So I managed to save the image, but I can't see it either from the gallery or my PC (using explorer)

this is my code:

 // Save bitmap to internal memory
private void savePhoto(Bitmap bmp){
    String appName = "myApp";

    String file_path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+ File.separator + appName;
    File dir = new File(file_path);

    if(!dir.exists())
        dir.mkdirs();   

    // Image file
    File file = new File(dir, "IMG" + "_" +     System.currentTimeMillis() + ".jpg");
    FileOutputStream out = null;

    try
    {
     out = new FileOutputStream(file);
     bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
     out.flush();
     out.close();

     out = null;
    } catch (Exception e)
    {
        e.printStackTrace();
    }

}

I can see it using some File Manager installed on my galaxy, but it doesn't help a lot. I know I should inform the gallery using a media scanner (which is another bridge I need to cross) but may someone help me understand way I can't even find the file..should I change its visibility somehow?

and one more thing: I read at another question regarding the issue, that I should add metadata to the image/folder so that the gallery would show it. Is it necessary?

Many thanks!!!

Upvotes: 0

Views: 498

Answers (2)

Splash
Splash

Reputation: 181

Apparently it did have something to do with the media scanner, I added this line

MediaScannerConnection.scanFile(this, new String[] { file.getAbsolutePath() }, null, null);

and now it works.

from the documentation:

MediaScannerConnection provides a way for applications to pass a newly created or downloaded media file to the media scanner service. The media scanner service will read metadata from the file and add the file to the media content provider.

Upvotes: 3

Vahe Muradyan
Vahe Muradyan

Reputation: 1115

try to replace this getExternalStoragePublicDirectory(....) with this one getExternalStorageDirectory();

Upvotes: 0

Related Questions