【问题标题】:fetching images from gallery on android phones with internal storage从具有内部存储的 android 手机上的图库中获取图像
【发布时间】:2013-08-14 07:00:25
【问题描述】:

您好,我正在开发一个 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) 的手机上运行此代码时,我得到的日志显示大小为零,即使内置图库中有图像。我该如何解决这个问题。 请帮忙。谢谢!

【问题讨论】:

  • 尝试将MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI更改为MediaStore.Images.Media.EXTERNAL_CONTENT_URI
  • 用户媒体由 EXTERNAL_CONTENT_URI 访问。 INTERNAL_CONTENT_URI 仅供系统应用使用。
  • 我尝试了 EXTERNAL_CONTENT_URI 和 INTERNAL_CONTENT_URI,EXTERNAL 在有 SD 卡的手机上运行良好,但在 Galaxy Nexus 等手机上无法运行(没有外部存储)
  • MediaStore.Images.Media.EXTERNAL_CONTENT_URI 是您的 URI。如果图片的缩略图不可用,那么您的查询将返回 0 数据。所以你查询的是媒体而不是缩略图。
  • 我尝试了 MediaStore.Images.Media.EXTERNAL_CONTENT_URI,这很好用,但从图库中获取 5 张图片也很慢!如何快速获取图片:MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI

标签: android gridview image-gallery android-gallery


【解决方案1】:

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

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

【讨论】:

  • managedQuery 已弃用。你应该使用 getContentResolver().query(...) 代替。
【解决方案2】:

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

   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 中设置选定的图像

【讨论】:

    【解决方案3】:
    String[] projection = {MediaStore.Images.Media._ID};
    

    MediaStore.Images.Thumbnails.INTERNAL_CONTENT_URI 替换为MediaStore.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 ) );
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-23
      • 1970-01-01
      相关资源
      最近更新 更多