Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
307 views
in Technique[技术] by (71.8m points)

android - Pre-guess size of Bitmap from the actual Uri, before scale-loading

If you have a Uri and you need the Bitmap, you could theoretically do this

Bitmap troublinglyLargeBmp =
  MediaStore.Images.Media.getBitmap(
      State.mainActivity.getContentResolver(), theUri );

but it will crash every time,

so you do this .........

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;

AssetFileDescriptor fileDescriptor =null;
fileDescriptor =
  State.mainActivity.getContentResolver().openAssetFileDescriptor( theUri, "r");

Bitmap actuallyUsableBitmap
  = BitmapFactory.decodeFileDescriptor(
    fileDescriptor.getFileDescriptor(), null, options);

Utils.Log("'4-sample' method bitmap ... "
   +actuallyUsableBitmap.getWidth() +" "
   +actuallyUsableBitmap.getHeight() );

that's fantastic and is the working solution.

Notice the factor of "four" which tends to work well, in current conditions (2014) with typical camera sizes, etc. HOWEVER, it would be best to guess or learn exactly the size of the image data at theUri, and then using that information, intelligently choose that factor.

In short, how to correctly choose that scale factor when you load a Uri ?

Android experts, is this a well-known problem, is there a solution? Thanks from your iOS->Android friends! :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

See the Android guide to handling large bitmaps.

Specifically this section:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), R.id.myimage, options);
int imageHeight = options.outHeight;
int imageWidth = options.outWidth;
String imageType = options.outMimeType;

EDIT

In your case, you'll replace the decodeResource line with:

BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...