is there any way to get thumbnail of images from USB OTG storage connected to android without copying full image

1.1k views Asked by At

I am trying to import selected images from usb drive connected to mobile phone (Nexus 6) which is not showing pendrive as external storage directories. I used this library: https://github.com/mjdev/libaums While using this library, I have to first copy all of the images from USB to local cache and from there I can show thumbnail to select which to import.I do not want to copy all the images I just want to get thumbnails and copy only selected images. So is there any way to get thumbnails from USB storage. I feel, ES file explorer and other explorer (whichever) are able to detect usb pendrive, they are not copying files to show thumbnails.

1

There are 1 answers

0
Qamar On

Yes you can,

Bitmap getThumbnail(UsbFile file,int targetWidth,int targetHeight){

        InputStream is = UsbFileStreamFactory.createBufferedInputStream(file,mSelectedDevice.getPartitions().get(0).getFileSystem());
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(is, null, options);

        Boolean scaleByHeight = Math.abs(options.outHeight - targetHeight) >= Math.abs(options.outWidth - targetWidth);

        if(options.outHeight * options.outWidth * 2 >= 200*200*2){
            // Load, scaling to smallest power of 2 that'll get it <= desired dimensions
            double sampleSize = scaleByHeight
                    ? options.outHeight / targetHeight
                    : options.outWidth / targetWidth;
            options.inSampleSize =
                    (int)Math.pow(2d, Math.floor(
                            Math.log(sampleSize)/Math.log(2d)));
        }

        // Do the actual decoding
        options.inJustDecodeBounds = false;

        // Let's load just the part of the image necessary for creating the thumbnail, not the whole image
        Bitmap thumbnail = BitmapFactory.decodeStream(is, null, options);
        return thumbnail;
    }

There are tons of opensource work available now. One of them is AnExplorer which support USB OTG storage. Check out AnExplorer