1

First of all, my permissions:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

My methods to save and get the image:

private void saveIamgeToLocalStore(Bitmap finalBitmap) { 
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/temp");    
        myDir.mkdirs(); 
        String fname = "Profile_Image.png";
        File file = new File (myDir, fname);
        if (file.exists()) file.delete(); 
        try {
            FileOutputStream out = new FileOutputStream(file);
            finalBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close(); 
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void loadImageFromLocalStore(String imageURI) { 
        try {
            Uri uri = Uri.parse(Environment.getExternalStorageDirectory().toString() + imageURI); 
            Bitmap bitmap = BitmapFactory.decodeStream(getActivity().getContentResolver().openInputStream(uri));
            profileImage.setImageBitmap(bitmap);
            profileImage.setTag("Other");
            select_image_button.setText(R.string.button_remove_profile_picture); 
        } catch (FileNotFoundException e) { 
            e.printStackTrace();
        } 
    }

Usage:

saveIamgeToLocalStore(BitmapFactory.decodeFile(picturePath));
loadImageFromLocalStore("/temp/Profile_Image.png");

I'm getting a

java.io.FileNotFoundException: No content provider: ... 

warning.

What am I missing?

PS: The image gets saved in /mnt/sdcard/temp/ . The warning appears when loading the image.

Vlad Manolache
  • 414
  • 2
  • 4
  • 25
  • Just check my answer for storing image at http://stackoverflow.com/questions/18010739/android-save-images-in-an-specific-folder/18010883#18010883 – mdDroid Aug 06 '13 at 06:57

2 Answers2

2

Is your file getting saved? In case yes, may be the mediascanner is not triggered before you do a read of the file. Since the mediascanner is not triggered, so content provider wont have the entry for your file (your file is not indexed).In case your file is getting saved with "saveIamgeToLocalStore", then trigger mediascanner from code once like this:

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

and then do a read on the file. It should work.

Mukesh Kumar Singh
  • 4,512
  • 2
  • 22
  • 30
Sushil
  • 8,250
  • 3
  • 39
  • 71
  • you can cross check this by just disconnecting the device after save and reconnecting it again which triggers media scanner. then just try to load and it will work. – Sushil Aug 06 '13 at 06:54
  • Thank you for your answer, but I am still getting the "java.io.FileNotFoundException: No content provider:" warning. Yes the image is saved as I said as /mnt/sdcard/temp/Profile_Image.png . I've tried both disconnecting and reconnecting and the code above. – Vlad Manolache Aug 06 '13 at 06:58
  • 1
    It turns out that I needed to append "file://" to the file path. SendBroadcast wasn't necessary, but your post helped me find the problem so I am accepting your answer. – Vlad Manolache Aug 06 '13 at 07:02
0

You need to write file existing check. Then it'll be easier to find problem. Short example

File test = new File (URI)
if ( test.exists() )
{
    // do your computation
} else 
{
    // find problem in file path
}
Sergey Brazhnik
  • 661
  • 4
  • 13