I m using an Uri returned from image gallery to create a Bitmap. I have a method that decodes the given Uri. When I call this method from the same Activity that got this Uri from onActivityResult it works fine. But if call the same method from any other Activity I get a FileNotFoundException.
What should I do to make this work in any Activity I want?
The method to decode:
public static Bitmap decodeUri(Context c, Uri uri, final int requiredSize)
throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);
int width_tmp = o.outWidth
, height_tmp = o.outHeight;
int scale = 1;
while(true) {
if(width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2);
}
Part of my Activity that calls the method above:
imageUri = data.getData();
myImage.setImageBitmap(UriDecoder.decodeUri(this, data.getData(), 170));
Logcat messages:
05-16 13:25:37.969: W/System.err(11480): java.io.FileNotFoundException: No content provider: /external/images/media/17
05-16 13:25:37.979: W/System.err(11480): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:508)
05-16 13:25:37.979: W/System.err(11480): at android.content.ContentResolver.openInputStream(ContentResolver.java:346)
05-16 13:25:37.979: W/System.err(11480): at com.utilities.UriDecoder.decodeUri(UriDecoder.java:16)
05-16 13:25:37.979: W/System.err(11480): at com.statistics.MyAdapter.getView(MyAdapter.java:55)
05-16 13:25:37.979: W/System.err(11480): at android.widget.AbsListView.obtainView(AbsListView.java:1442)
05-16 13:25:37.979: W/System.err(11480): at android.widget.ListView.measureHeightOfChildren(ListView.java:1216)
05-16 13:25:37.989: W/System.err(11480): at android.widget.ListView.onMeasure(ListView.java:1127)
05-16 13:25:37.989: W/System.err(11480): at android.view.View.measure(View.java:8325)
05-16 13:25:37.989: W/System.err(11480): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 13:25:37.989: W/System.err(11480): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1017)
05-16 13:25:37.989: W/System.err(11480): at android.widget.LinearLayout.measureVertical(LinearLayout.java:386)
05-16 13:25:37.989: W/System.err(11480): at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-16 13:25:37.989: W/System.err(11480): at android.view.View.measure(View.java:8325)
05-16 13:25:37.999: W/System.err(11480): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 13:25:37.999: W/System.err(11480): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-16 13:25:37.999: W/System.err(11480): at android.view.View.measure(View.java:8325)
05-16 13:25:37.999: W/System.err(11480): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 13:25:37.999: W/System.err(11480): at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-16 13:25:38.009: W/System.err(11480): at android.view.View.measure(View.java:8325)
05-16 13:25:38.009: W/System.err(11480): at android.view.ViewRoot.performTraversals(ViewRoot.java:847)
05-16 13:25:38.009: W/System.err(11480): at android.view.ViewRoot.handleMessage(ViewRoot.java:1883)
05-16 13:25:38.009: W/System.err(11480): at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 13:25:38.009: W/System.err(11480): at android.os.Looper.loop(Looper.java:130)
05-16 13:25:38.009: W/System.err(11480): at android.app.ActivityThread.main(ActivityThread.java:3737)
05-16 13:25:38.009: W/System.err(11480): at java.lang.reflect.Method.invokeNative(Native Method)
05-16 13:25:38.019: W/System.err(11480): at java.lang.reflect.Method.invoke(Method.java:507)
05-16 13:25:38.019: W/System.err(11480): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:894)
05-16 13:25:38.019: W/System.err(11480): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
05-16 13:25:38.019: W/System.err(11480): at dalvik.system.NativeStart.main(Native Method)