获取图像缩略图文件路径

5

我想获取缩略图路径,而不是位图对象。
查询时,某些缩略图路径出现了null的情况。(我的设备上有1028个缩略图。光标长度确实为1028,但仍然返回null)我知道有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;
 }

这是我的logcat日志: http://pastebin.com/UZLZF9Pg 经过检查,我发现我发送的id就像for循环的索引一样。我甚至不确定我的代码是否能正常工作,因此任何其他代码都将是很好的选择。

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

11

你应该查询 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();

参考资料:如何从图像缩略图路径获取图像路径?


太好了,运行正常!!!非常感谢,我一直在寻找这个解决方案 :) - idish
嘿,希望你能帮我解决一个小问题:我刚刚删除了所有的相册图片,然后用相机拍了大约12张照片。之后,我进入了相册,看到缩略图显示得很好。然而,当我进入我的应用程序来显示这些缩略图时,我却看不到任何东西。在查看DCIM中的.Thumbnails文件夹后,根本没有jpg文件,只有一个奇怪的文件,重量为10.9 MB。你知道为什么我的.Thumbnails文件夹不显示我的缩略图吗? - idish
@idish 请将其作为单独的问题打开并提供更多细节(“奇怪文件”的名称是什么),还需要提供手机详细信息和操作系统版本。 - yogurtearl
我发现只有少数图片有缩略图,不知道为什么。 - Allen Vork

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接