我正在使用从图库中返回的Uri来创建位图。我有一个方法解码给定的Uri。当我从onActivityResult获得此Uri的同一个Activity调用此方法时,它可以正常工作。但是如果从任何其他Activity调用相同的方法,我会得到一个FileNotFoundException。
我应该怎么做才能在我想要的任何活动中使用它?
解码方法:
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);
}
调用上述方法的部分活动:
imageUri = data.getData();
myImage.setImageBitmap(UriDecoder.decodeUri(this, data.getData(), 170));
Logcat消息:
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)
答案 0 :(得分:2)
请尝试使用此内容URI:
content://media/external/images/media/