2

I have Moto X Pure 2015 with Android 6.0 on the board. Before updating to Android 6 my code of obtaining thumbnail of last captured image on external storage are worked fine. More over, that code works on all devices I've tested (several tens). I can't localize what's changed in Android 6 to broke my code in such way. All dynamic permission is granted and 'write to external storage' too. So, that's what I do to get thumbnail. First I query Cursor and get _id of first founded file (Media - it's just a container of some data, including _id of founded image):

        Uri baseUri = Images.Media.EXTERNAL_CONTENT_URI;
        Uri query = baseUri.buildUpon().appendQueryParameter("limit", "1").build();
        String[] projection = new String[] { ImageColumns._ID, ImageColumns.ORIENTATION, ImageColumns.DATE_TAKEN };

        String selection = ImageColumns.DATA + " like '%" + dirName + "%' AND " + ImageColumns.MIME_TYPE
                + "='image/jpeg'";
        String order = ImageColumns.DATE_TAKEN + " DESC," + ImageColumns._ID + " DESC";

        Cursor cursor = null;

        try
        {
            cursor = resolver.query(query, projection, selection, null, order);
            if (cursor != null && cursor.moveToFirst())
            {
                final long id = cursor.getLong(0);
                externalMedia = new Media(id, cursor.getInt(1), cursor.getLong(2), ContentUris.withAppendedId(
                        baseUri, id));
            }
        } finally
        {
            if (cursor != null)
            {
                cursor.close();
            }
        }

Now I try to get Bitmap for image with that _id (externalMedia container contains that id):

Bitmap bitmap = Images.Thumbnails.getThumbnail(resolver, externalMedia.id, Images.Thumbnails.MICRO_KIND, null);

And bitmap is null ((((

I didn't found any changes in Android 6 from Android 5 in ContentResolver and Images.Thumbnails parts. So now I'm stuck with that issue.

Grinchman
  • 243
  • 4
  • 14
  • It seems you need to request permission at runtime: See: http://stackoverflow.com/a/33618259/5778152 – Nicolas Cortell Feb 02 '16 at 12:41
  • I request all necessary permissions at runtime at start of application. I've checked twice read\write to external storage permission - it's granted. – Grinchman Feb 03 '16 at 07:23

1 Answers1

0

I had similar problem and found solution, I use observer pattern.

public interface IPhotoSavedObserver {
    void onPhotoSaved(String path, Bitmap bitmap);
}

class Image Model

public ImageModel(String path){
    this.mPath = path;
    Bitmap bitmap = BitmapFactory.decodeFile(path);
    ThumbnailSizeProvider sizeProvider = ThumbnailSizeProvider.getInstance();
    mThumbNail = ThumbnailUtils.extractThumbnail(bitmap,sizeProvider.getWidth(),sizeProvider.getHeight());
}

public Bitmap getThumbNail() {

    return mThumbNail;
}

public void initThumbnail(){
    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {
            Bitmap bitmap = BitmapFactory.decodeFile(mPath);
            for(int i=0;i<10;i++) {
                if (bitmap == null) {
                    try {
                        Thread.sleep(400);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    bitmap = BitmapFactory.decodeFile(mPath);
                }
                if(bitmap!=null) {
                    ThumbnailSizeProvider sizeProvider = ThumbnailSizeProvider.getInstance();
                    mThumbNail = ThumbnailUtils.extractThumbnail(bitmap, sizeProvider.getWidth(), sizeProvider.getHeight());
                    PhotoSavedObservable.getInstance().notifyObservers(getPath(), mThumbNail);
                    i=10;
                }
            }
        }
    });
    t.start();
}

in my adapter at getView method

        ImageModel iModel = mItems.get(position);
        mImagesMap.put(iModel.getPath(),imgView);
        Bitmap thumb = iModel.getThumbNail();
        if(thumb == null)
            iModel.initThumbnail();
        else
            imgView.setImageBitmap(thumb);

adapter is inherited from IPhotoSavedObserver

@Override
public void onPhotoSaved(String path, final Bitmap bitmap) {
    if(mImagesMap.containsKey(path)){
        final ImageView imgView = mImagesMap.get(path);
        Activity activity = (Activity) getContext();
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                imgView.setImageBitmap(bitmap);
            }
        });
    }
}

And all works correctly :)

Abbath
  • 1,002
  • 13
  • 30