0

我正在尝试使用光标从我这里读取 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)
4

0 回答 0