使用ContentResolver和Cursor在android中获取图像的缩略图

发布于 2024-12-21 23:57:47 字数 2299 浏览 0 评论 0原文

以下是我用于获取给定图像的缩略图的代码。 截至目前,我遇到的异常是“光标越界”,

我认为这可能是因为我没有在任何地方附加图像 URL。我有点困惑不知道在哪里可以做到这一点。 所以有2个问题: 1. 在哪里使用我想要获取缩略图的图片URL 2. 应该打印列名的 for 循环除了第一个语句“COLUMN NAMES”外什么也不打印

        //get the corresponding thumbnail
                String lastImageTakenPath = MyActivity.this.savedInstanceStateVariable.getString("lastImageTaken");
                System.out.println("previous image is "+ lastImageTakenPath);                   

        ContentResolver cr = getContentResolver();
        if(cr != null){


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

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



        //Cursor cursor = cr.query(lastImageTakenURI, null, null, null, null);
        //Activity.startManagingCursor(cursor);
         if(cursor != null){
        String[] columnNames = cursor.getColumnNames();

                        System.out.println("COLUMN NAMES");
        for(int i=0;i<columnNames.length; i++){
           System.out.println(columnNames[i]);
         }


          /* 1. get the id of the image
                * 2. use this id in the call, getThumbnails on MediaStore.Images.Thumbnails to obtain the 
                            thumbnail
         3.set the imageview's src to this thumbnail */

        int imageID = cursor.getInt( cursor.getColumnIndex(MediaStore.Images.Media._ID) ); 

         Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
                       // Get original image ID  
                       String url = uri.toString();
                       int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
        // Get (or create upon demand) the micro thumbnail for the original image.    
          thumbnailLastImage = MediaStore.Images.Thumbnails.getThumbnail(cr, originalImageId, MediaStore.Images.Thumbnails.MICRO_KIND,null);

         thumbnailImage.setImageBitmap(thumbnailLastImage);



          } 
          else{
            System.out.println("Cursor is NULL");
             }
    }
    else{
      Log.d(TAG,"ContentResolver is NULL");
    }

The following is my code for obtaining a thumbnail of a given image.
As of now, the exception that I'm getting is 'Cursor out of bounds'

I think that may be because I am not appending the image URL anywhere. I'm a little confused as to where to do that.
So 2 questions:
1. Where do I use the image URL of which I want to obtain a thumbnail
2. The for loop which is supposed to print column names prints nothing except the first statement 'COLUMN NAMES'

        //get the corresponding thumbnail
                String lastImageTakenPath = MyActivity.this.savedInstanceStateVariable.getString("lastImageTaken");
                System.out.println("previous image is "+ lastImageTakenPath);                   

        ContentResolver cr = getContentResolver();
        if(cr != null){


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

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



        //Cursor cursor = cr.query(lastImageTakenURI, null, null, null, null);
        //Activity.startManagingCursor(cursor);
         if(cursor != null){
        String[] columnNames = cursor.getColumnNames();

                        System.out.println("COLUMN NAMES");
        for(int i=0;i<columnNames.length; i++){
           System.out.println(columnNames[i]);
         }


          /* 1. get the id of the image
                * 2. use this id in the call, getThumbnails on MediaStore.Images.Thumbnails to obtain the 
                            thumbnail
         3.set the imageview's src to this thumbnail */

        int imageID = cursor.getInt( cursor.getColumnIndex(MediaStore.Images.Media._ID) ); 

         Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(imageID) );
                       // Get original image ID  
                       String url = uri.toString();
                       int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));
        // Get (or create upon demand) the micro thumbnail for the original image.    
          thumbnailLastImage = MediaStore.Images.Thumbnails.getThumbnail(cr, originalImageId, MediaStore.Images.Thumbnails.MICRO_KIND,null);

         thumbnailImage.setImageBitmap(thumbnailLastImage);



          } 
          else{
            System.out.println("Cursor is NULL");
             }
    }
    else{
      Log.d(TAG,"ContentResolver is NULL");
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小矜持 2024-12-28 23:57:47

我相信您对 if(cursor != null) 的测试不正确或至少不充分。如果查询结果没有返回缩略图,那么您仍然会得到一个游标,其中cursor.getCount() == 0,您可能希望将其用作测试。

I believe your test of if(cursor != null) is incorrect or at least insufficient. If the result of the query returns no thumbnails then you will still get a cursor where cursor.getCount() == 0 you may want to use that as your test.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文