0

我想了解如何使用光标在 sdcard 中指定的文件夹中生成 jpg 文件。我试图用光标选择特定文件夹中的 jpg 文件,我尝试了这个:

这是代码:

public static Uri getRandomImage(ContentResolver resolver) {

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

    String folder = "/sdcard/DCIM/Wallpaper/";
    folder = folder + "%";
    Uri uri = Media.EXTERNAL_CONTENT_URI;
    String[] whereArgs = new String[]{folder};

    Cursor cursor = resolver.query(uri, projection, null, 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/AndroidRuntime(11986): FATAL EXCEPTION: main
E/AndroidRuntime(11986): android.database.sqlite.SQLiteException: bind or column index out of range: handle 0x26a490
E/AndroidRuntime(11986):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158)
E/AndroidRuntime(11986):    at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
E/AndroidRuntime(11986):    at android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330)
E/AndroidRuntime(11986):    at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
E/AndroidRuntime(11986):    at android.content.ContentResolver.query(ContentResolver.java:245)
E/AndroidRuntime(11986):    at it.bisemanuDEV.slidepuzzle.SelectImagePreference.getRandomImage(SelectImagePreference.java:126)
E/AndroidRuntime(11986):    at it.bisemanuDEV.slidepuzzle.TileView.newGame(TileView.java:156)
E/AndroidRuntime(11986):    at it.bisemanuDEV.slidepuzzle.SlidePuzzleActivity.onOptionsItemSelected(SlidePuzzleActivity.java:377)
E/AndroidRuntime(11986):    at android.app.Activity.onMenuItemSelected(Activity.java:2762)
E/AndroidRuntime(11986):    at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730)
E/AndroidRuntime(11986):    at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:143)
E/AndroidRuntime(11986):    at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855)
E/AndroidRuntime(11986):    at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:532)
E/AndroidRuntime(11986):    at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122)
E/AndroidRuntime(11986):    at android.view.View$PerformClick.run(View.java:8819)
E/AndroidRuntime(11986):    at android.os.Handler.handleCallback(Handler.java:603)
E/AndroidRuntime(11986):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(11986):    at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(11986):    at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(11986):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(11986):    at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(11986):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(11986):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(11986):    at dalvik.system.NativeStart.main(Native Method)
4

2 回答 2

0

当我理解你的正确时,你想用你的 sd 卡中的图像设置一个 imageView。如果是这样,您可以像我的示例中那样进行操作:

 ImageView shopLogo = (ImageView) findViewById(R.id.img);
 Resources res = getResources();
 Bitmap bmp = BitmapFactory.decodeFile(YOUR_SDCARD_PATH);
 BitmapDrawable bd = new BitmapDrawable(res, bmp);
 shopLogo.setBackgroundDrawable(bd);

YOUR_SDCARD_PATH 包含:

Environment.getExternalStorageDirectory().getAbsolutePath() + "/Directory/image.jpg";


Environment.getExternalStorageDirectory().getAbsolutePath()

为您提供通往 SD 卡的路径。

于 2012-09-10T15:03:12.007 回答
0

从你有的例外:

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

更准确地说:

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

正在返回游标范围之外的 cursor.getCount()。

来自Random().nextInt(n) java文档:

Returns a pseudo-random uniformly distributed int in the half-open range [0, n).

这意味着它可以返回 n。所以如果你想让你的代码工作,试试,new Random().nextInt(cursor.getCount() -1)因为你已经测试过了cursor.getCount() > 0

于 2012-10-23T08:45:36.677 回答