繁体   English   中英

使用gridview从SD卡显示图像

[英]Displaying Images from SD Card using gridview

我正在开发一个应用程序,其中包含使用网格视图的图像库,并且我尝试了很多代码。 我已在清单中请求许可。 但这给了我这条线上的错误,并且告诉我managedQuery已过时。

cursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,projection,null,null,MediaStore.Images.Thumbnails.IMAGE_ID);

是不推荐使用的方法导致错误还是由于游标引起的?


public class MainActivity extends Activity {

//  Cursor used to access the results from querying for images on the SD card.

private Cursor cursor;

// Column index for the Thumbnails Image IDs.

private int columnIndex;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Set up an array of the Thumbnail Image ID column we want
    String[] projection = {MediaStore.Images.Thumbnails._ID};
    // Create the cursor pointing to the SDCard
    cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            projection, // Which columns to return
            null,       // Return all rows
            null,
            MediaStore.Images.Thumbnails.IMAGE_ID);
    // Get the column index of the Thumbnails Image ID
    columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);

    GridView sdcardImages = (GridView) findViewById(R.id.gridView1);
    sdcardImages.setAdapter(new ImageAdapter(this));


}

private class ImageAdapter extends BaseAdapter {

    private Context context;

    public ImageAdapter(Context localContext) {
        context = localContext;
    }
    public int getCount() {
        return cursor.getCount();
    }
    public Object getItem(int position) {
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView picturesView;
        if (convertView == null) {
            picturesView = new ImageView(context);
            // Move cursor to current position
            cursor.moveToPosition(position);
            // Get the current value for the requested column
            int imageID = cursor.getInt(columnIndex);
            // Set the content of the image based on the provided URI
            picturesView.setImageURI(Uri.withAppendedPath(
                    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
            picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
            picturesView.setPadding(10, 10, 10, 10);
            picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
        }
        else {
            picturesView = (ImageView)convertView;
        }
        return picturesView;
    }
}
}

     02-24 20:07:25.632 25424-25424/? I/art: Not late-enabling -Xcheck:jni (already on)
    02-24 20:07:25.634 25424-25424/? W/art: Unexpected CPU variant for X86 using defaults: x86
    02-24 20:07:25.752 25424-25424/com.project.nuha.sdtotorial W/System: 
    ClassLoader referenced unknown path: /data/app/com.project.nuha.sdtotorial-
     1/lib/x86
    02-24 20:07:26.022 25424-25424/com.project.nuha.sdtotorial D/AndroidRuntime: 
     Shutting down VM
02-24 20:07:26.024 25424-25424/com.project.nuha.sdtotorial E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.project.nuha.sdtotorial, PID: 25424
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.nuha.sdtotorial/com.project.nuha.sdtotorial.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/thumbnails from pid=25424, uid=10092 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                 at android.os.Looper.loop(Looper.java:154)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
                                                                              Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/thumbnails from pid=25424, uid=10092 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                                                                                 at android.os.Parcel.readException(Parcel.java:1683)
                                                                                 at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                                                                                 at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                                                                                 at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
                                                                                 at android.content.ContentResolver.query(ContentResolver.java:530)
                                                                                 at android.content.ContentResolver.query(ContentResolver.java:472)
                                                                                 at android.app.Activity.managedQuery(Activity.java:2234)
                                                                                 at com.project.nuha.sdtotorial.MainActivity.onCreate(MainActivity.java:36)
                                                                                 at android.app.Activity.performCreate(Activity.java:6662)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                 at android.os.Looper.loop(Looper.java:154) 
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866) 
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

在这种情况下,根本例外是:

Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/thumbnails from pid=25424, uid=10092 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

您需要请求READ_EXTERNAL_STORAGE权限,包括处理运行时权限。 文档中介绍了权限的使用,以及有关Android应用程序开发的书籍和课程。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM