7

嗨,我正在开发一个 Android Gallery 应用程序,我正在从内置图库中获取图像并显示它。我正在使用如下代码

        String[] projection = {MediaStore.Images.Thumbnails._ID};


   Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,       
                null);


        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
        int size = cursor.getCount();
        // If size is 0, there are no images on the SD Card.
        if (size == 0) 
        {
            Log.e("size 0","0");
        }

问题是当我在只有内部存储(Galaxy Nexus)的手机上运行此代码时,我得到的日志显示大小为零,即使内置图库中有图像。我该如何解决这个问题。请帮助。谢谢!

4

3 回答 3

4

要获取图库图片列表,您可以试试这个

String[] projection = new String[]{
        MediaStore.Images.Media._ID,
        MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
        MediaStore.Images.Media.DATE_TAKEN
};


Uri imageUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;


Cursor cur = getContentResolver().query(imageUri,
        projection, // Which columns to return
        null,       // Which rows to return (all rows)
        null,       // Selection arguments (none)
        null        // Ordering
        );

Log.i("Images Count"," count="+cur.getCount());
于 2013-08-14T07:09:44.590 回答
3

在你的按钮上试试这个,比如“浏览”

   browse.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {

         Intent i = new Intent(
                 Intent.ACTION_PICK,
                 android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                 startActivityForResult(i, RESULT_LOAD_IMAGE);

    }
});

并且您还将所选图像设置为 ImageView

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            picturePath = cursor.getString(columnIndex);
            cursor.close();

             imageView = (ImageView) findViewById(R.id.property_image);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

}

在第一个代码块中,我startActivityForResult(i, RESULT_LOAD_IMAGE);将此返回结果用于调用的活动,我们可以通过第二个代码块获得此结果,并在您的 ImageView 中设置选定的图像

于 2013-08-14T07:18:54.000 回答
2
String[] projection = {MediaStore.Images.Media._ID};

替换MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URIMediaStore.Images.MEDIA.EXTERNAL_CONTENT_URI

   Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                projection, // Which columns to return
                null,       // Return all rows
                null,       
                null);


    int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
    int size = cursor.getCount();
    // If size is 0, there are no images on the SD Card.
    if (size == 0) 
    {
        Log.e("size 0","0");
    }

编辑:获取缩略图列表并从光标处获取图像 URI


Uri uri=MediaStore.Images.Thumbnails.getContentUri("external");

 Cursor cursor=MediaStore.Images.Thumbnails.queryMiniThumbnails
      (getContentResolver(), uri, MediaStore.Images.Thumbnails.MINI_KIND,null);

if( cursor != null && cursor.getCount() > 0 ) {
    String uri = cursor.getString( cursor.getColumnIndex( MediaStore.Images.Thumbnails.DATA ) );
 }
于 2013-08-14T10:07:58.187 回答