How to read jpg file from a specific location with the cursor

◇◆丶佛笑我妖孽 提交于 2020-01-03 05:28:11

问题


I would like to understand how I can use a cursor to jpg files in a folder specified in the sdcard. I'm trying to select with a cursor the jpg files in a specific folder, and I tried this:

This is the code:

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));
}

but this code gives me error.

Here is the logcat:

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)

回答1:


When I understand you right, you want to set an imageView with an image from your sd card. If it is this you can do it like in my example:

 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);

The YOUR_SDCARD_PATH contains:

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


Environment.getExternalStorageDirectory().getAbsolutePath()

gives you back the path to the sdcard.




回答2:


From the exception you have:

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

more precisly :

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

is returning cursor.getCount() which is outside of the range of the cursor.

From Random().nextInt(n) java doc:

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

This means it can return n. So if you want to make your code work, try new Random().nextInt(cursor.getCount() -1) since you already tested that cursor.getCount() > 0



来源:https://stackoverflow.com/questions/12353766/how-to-read-jpg-file-from-a-specific-location-with-the-cursor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!