I am having weird problem, I am having a user click button to choose an image. Then I am storing the encoded path in db and retreiving it later so I display the image in another activity but I am getting the following Exception
09-14 01:39:36.195: W/System.err(2241): java.io.FileNotFoundException: No content provider: /external/images/media/1113
09-14 01:39:36.195: W/System.err(2241): at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:610)
09-14 01:39:36.195: W/System.err(2241): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:542)
09-14 01:39:36.195: W/System.err(2241): at android.content.ContentResolver.openInputStream(ContentResolver.java:377)
My button OnClick & store in db code:
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream, null, options );
ivPictureChosen.setImageBitmap(yourSelectedImage);
storeEncodedImageInDb(selectedImage.getEncodedPath());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "Couldn't pick image", Toast.LENGTH_SHORT).show();
}
My retreival & showing image code:
String imagePath = db.getEncodedImage();
if(imagePath.length()>0){
Uri mainImgeUri = Uri.parse(imagePath);
InputStream imageStream;
try {
imageStream = getContentResolver().openInputStream(mainImgeUri);
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream, null, options );
mainImage.setImageBitmap(yourSelectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
How come the file is not found although thats the encoded path I got in the first place?
You will not only need the path but also scheme etc.
An Uri looks like so: http://www.stackoverflow.com or file://foo/bar/image.jpg.
encodedPath is only the path part of this, e.g. stackoverflow.com or /foo/bar/image.jpg.
Try to store the result of
yourUri.toString()in the database and retrieveUri.parse(theStringFromDatabase)