3

我正在使用从图片库返回的 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)

4

1 回答 1

2

请尝试使用此内容 URI:

content://media/external/images/media/
于 2012-05-16T11:07:12.760 回答