【问题标题】:Get images thumbnail file paths获取图片缩略图文件路径
【发布时间】:2013-08-12 21:13:05
【问题描述】:

我正在尝试获取缩略图路径,而不是位图对象。
当我查询这些时,某些缩略图路径由于某种原因为空。 (我的设备中有 1028 个缩略图,光标长度确实是 1028,但仍然返回空值)我知道有 1028 个缩略图,因为我检查过。 这是我的代码:

     String[] projection = {MediaStore.Images.Thumbnails._ID};
  // Create the cursor pointing to the SDCard

  cursor = this.getContentResolver().query( 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
  Log.d(Global.TAG, "BEFORE");
  columnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails._ID);
  Log.d(Global.TAG, "AFTER1");
  for(int i =0;i<cursor.getCount();i++){
      cursor.moveToPosition(i);

      Log.d("MyTag","BBABA" + i +" : " + getThumbnailPathForLocalFile(cursor.getLong(columnIndex)));
  }
  cursor.close();

我的 getThumbnailPathForLocalFile:

    String getThumbnailPathForLocalFile(long fileId)
 {
    // Log.d(Global., msg)
     Cursor thumbCursor = null;
     try
     {
         thumbCursor = this.getContentResolver().
                 query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI
                 , null
                 , MediaStore.Images.Thumbnails.IMAGE_ID + " = " + fileId+ " AND "
                   + MediaStore.Images.Thumbnails.KIND + " = "
                   + MediaStore.Images.Thumbnails.MINI_KIND , null, null);

         if(thumbCursor.moveToFirst())
         {
             // the path is stored in the DATA column
             int dataIndex = thumbCursor.getColumnIndexOrThrow( MediaStore.MediaColumns.DATA );
             String thumbnailPath = thumbCursor.getString(dataIndex);
             return thumbnailPath;
         }
     }
     finally
     {
         if(thumbCursor != null)
         {
             thumbCursor.close();
         }
     }

     return null;
 }

这是我的日志: http://pastebin.com/UZLZF9Pg

检查后,我看到我发送的 id 就像 for 循环的索引一样。 我什至不确定我的代码是否应该工作,所以任何其他代码都会很棒。

【问题讨论】:

  • 为什么将 PROJECTION 设置为 MediaStore.Images.Thumbnails._ID 而不是 MediaStore.Images.Thumbnails.DATA?
  • @IgorGanapolsky 我不确定。我想我在某个地方拿了那个例子。你认为它可以解决问题吗?无论如何,它已经无关紧要了。
  • 我之所以问,是因为我使用了 {MediaStore.Images.Thumbnails.DATA},就像在解决方案中一样,并且它有效。所以我很好奇你是否有不同的方法。
  • 呃,如果你几个月前告诉我,你可以解决我很多问题.. :(

标签: android path media thumbnails android-cursor


【解决方案1】:

您应该查询MediaStore.Images.Thumbnails.DATA。要修改您的示例,它看起来像这样。

String[] projection = {MediaStore.Images.Thumbnails.DATA};
// Create the cursor pointing to the SDCard

    Cursor cursor = this.getContentResolver().query( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
        projection, // Which columns to return
        null,       // Return all rows
        null,
        null);
// Get the column index of the Thumbnails Image ID
Log.d(TAG, "BEFORE");
int columnIndex = cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA);
Log.d(TAG, "AFTER1");
for(int i =0;i<cursor.getCount();i++){
    cursor.moveToPosition(i);

    Log.d("MyTag","BBABA" + i +" : " + cursor.getString(columnIndex));
}
cursor.close();

参考:How to get imagepath from thumbnail path of a image?

【讨论】:

  • 这很棒,工作!!!非常感谢,我一直在寻找这个解决方案:)
  • 嘿,希望您能帮我解决另一个小问题:我刚刚删除了所有图库图片,然后用相机拍摄了大约 12 张图片。之后,我进入画廊,看到缩略图显示得很好。但是,当我进入我的应用程序以显示这些缩略图时,我什么也看不到。查看 DCIM 中的 .Thubnails 文件夹后,根本没有 jpg 文件,只有一个 10.9 MB 的奇怪文件。你知道为什么我的 .Thumbnails 文件夹不显示我的缩略图吗?
  • @idish 请将其作为一个单独的问题打开并提供更多详细信息(“奇怪文件”的名称是什么),以及手机详细信息和操作系统版本。
  • 我发现只有几张图片有缩略图。我不知道为什么
猜你喜欢
  • 2017-03-10
  • 1970-01-01
  • 2013-01-01
  • 2012-03-05
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 2012-05-15
  • 1970-01-01
相关资源
最近更新 更多