如何从指定的uri读取图片文件

时间:2012-09-10 13:25:25

标签: android android-layout android-intent android-widget

我试图用光标读取我在URI中指定的文件。但我无法弄清楚错误似乎从我指示的路径中读取

这是代码:

 public static Uri getRandomImage(ContentResolver resolver) {

    String[] projection = new String[] {
        BaseColumns._ID,

    };


    Uri uri = Media.getContentUri("/sdcard/DCIM/Wallpaper/");

    String[] selectionArgs = null; // there is no ? in selection so null here

    String sortOrder = null; // unordered
    Cursor cursor = resolver.query(uri, projection, null, selectionArgs, sortOrder);

    if (cursor == null || cursor.getCount() <= 0) {
            return null;
    }

    cursor.moveToPosition(new Random().nextInt(cursor.getCount()));

    return Uri.withAppendedPath(uri, cursor.getString(0));
}

这是出现新错误的代码:

ParcelFileDescriptor pfd;

    try {
        pfd = context.getContentResolver().openFileDescriptor(uri, "r"); //line with error
    } catch(FileNotFoundException fnfe) {
        Log.e(LOG_TAG, Log.getStackTraceString(fnfe));
        return null;
    }

这是日志猫:

E/it.bisemanuDEV.slidepuzzle.TileView(11188): java.io.FileNotFoundException: No content provider: 
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:464)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:380)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at it.bisemanuDEV.slidepuzzle.TileView.getImageFromUri(TileView.java:560)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at it.bisemanuDEV.slidepuzzle.TileView.onDraw(TileView.java:239)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.View.draw(View.java:6741)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.ViewGroup.drawChild(ViewGroup.java:1638)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.View.draw(View.java:6744)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.View.draw(View.java:6744)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.ViewGroup.drawChild(ViewGroup.java:1640)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.ViewGroup.dispatchDraw(ViewGroup.java:1367)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.View.draw(View.java:6744)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.widget.FrameLayout.draw(FrameLayout.java:352)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1842)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.ViewRoot.draw(ViewRoot.java:1407)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.ViewRoot.performTraversals(ViewRoot.java:1163)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.os.Handler.dispatchMessage(Handler.java:99)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.os.Looper.loop(Looper.java:123)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at android.app.ActivityThread.main(ActivityThread.java:4627)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at java.lang.reflect.Method.invokeNative(Native Method)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at java.lang.reflect.Method.invoke(Method.java:521)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/it.bisemanuDEV.slidepuzzle.TileView(11188):   at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

课程MediaStore.Files仅在API级别11之后可用(请参阅http://developer.android.com/reference/android/provider/MediaStore.html)。

如果您尝试在Android版本较低的设备上使用它,则会出现此错误。

答案 1 :(得分:0)

您可能误解了volumeName方法的getContentUri()参数。

它应该是“内部”或“外部”之一(见What does MediaStore.Images.Media.getContentUri(String volumeName):Uri do?