我已经在我的Android应用程序中包含了来自gallery选项的照片,代码附在下面。
Intent intent = getIntent();
Uri data = intent.getData();
// Figure out what to do based on the intent type
if (intent.getType().indexOf("image/") != -1) {
// Handle intents with image data ...
Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
Context context1=this;
Cursor cursor = null;
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context1.getContentResolver().query(imageUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path=cursor.getString(column_index);
name=path.substring(path.lastIndexOf("/")+1);
mBitmap =MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
ByteArrayOutputStream bos=new ByteArrayOutputStream();
mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
img = bos.toByteArray();
strImage = Base64.encodeToString(img, Base64.DEFAULT);
但是当我使用大图像时,应用程序崩溃了。我按照this问题解决了问题,但对以下代码中提到的所需大小仍有疑问
static Bitmap decodeUri(Context c, Uri uri, final int requiredSize)
throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o);
int width_tmp = o.outWidth
, height_tmp = o.outHeight;
int scale = 1;
while(true) {
if(width_tmp / 2 < requiredSize || height_tmp / 2 < requiredSize)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(c.getContentResolver().openInputStream(uri), null, o2);
}
是否有任何选项可以按照以下简单方法调整图像大小
//Convert Bitmap to Byte Array:-
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
bitmap = BitmapFactory.decodeFile(filePath,options);
如果没有这样的方法,那么代码中提到的所需大小是什么,以便应用程序不会崩溃。请帮我解决这个问题..