我在阅读一些我在此路径中的文件夹中复制的图像文件时出现问题:/ 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));
}
logcat的:
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)
答案 0 :(得分: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);
EDIT2:
在getContentResolver
中,您将返回null。 getContentResolver
是Context的一种方法。使用context.getContentResolver