1

以下は、特定の画像のサムネイルを取得するための私のコードです。今のところ、私が得ている例外は「カーソルが範囲外です」です

これは、画像の 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");
    }

4

1 に答える 1

-2

if(cursor != null) のテストが正しくないか、少なくとも不十分だと思います。クエリの結果がサムネイルを返さない場合でも、cursor.getCount() == 0 のカーソルが表示されます。これをテストとして使用することをお勧めします。

于 2013-12-11T00:08:26.707 に答える