1

我在读取我在此路径的文件夹中复制的一些图像文件时遇到问题:/sdcard/DCIM/Wallpaper/。

问题在于随机读取这些图像:

我正在使用这种方法,但似乎光标没有读取我传递的 URI,发生了什么?

public static Uri getRandomImage(ContentResolver resolver) {

    String[] projection = new String[] {
        BaseColumns._ID
    };


    String folder = "/sdcard/DCIM/Wallpaper/";
    folder = folder + "%";
    String where = Media.DATA + " LIKE ?";
    String[] whereArgs = new String[]{folder};

    Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        projection, where, whereArgs, MediaColumns._ID);


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

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

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

日志猫:

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

1 回答 1

0

您正在尝试获取目录的 Uri,该目录不会出现在 Media Uri 中。所以你得到了 FilenotfoundException。

您必须进行自定义查询才能仅获取目录中存在的图像

编辑:从未尝试过,但请尝试一下。where 子句应该使用 LIKE。如果这不起作用,您可能不得不环顾四周以构造 LIKE 子句。

String folder = "/sdcard/DCIM/Wallpaper/";
folder = folder + "%";
String where = Media.DATA + " LIKE ?";
String[] whereArgs = new String[]{folder};

cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
    projection, where, whereArgs, MediaColumns._ID);

编辑2:

getContentResolver你返回为空。getContentResolver是上下文的一种方法。利用context.getContentResolver

于 2012-09-08T09:40:02.360 回答