在某些设备中拍摄照片android的OutOfMemeryError

在某些设备中拍摄照片android的OutOfMemeryError,第1张

概述在某些设备和某些版本的Android中用相机拍照时,我遇到问题.例如,在我的XperiaUv4.0.3中,我的代码可以正常工作,但在另一个xperiaUv2.3.3中,则无法正常工作…我读到很多有关此错误的信息,但无法解决…我拍摄照片并显示的代码:publicvoidcallCamera(){Intentintent=new

在某些设备和某些版本的Android中用相机拍照时,我遇到问题.例如,在我的Xperia U v4.0.3中,我的代码可以正常工作,但在另一个xperia U v2.3.3中,则无法正常工作…

我读到很多有关此错误的信息,但无法解决…

我拍摄照片并显示的代码:

public voID callCamera(){Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);file photo = new file(Environment.getExternalStorageDirectory(),  "Pic.jpg");intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromfile(photo));imageUri = Uri.fromfile(photo);startActivityForResult(intent, SELECT_CAMERA);}public voID onActivityResult(int requestCode, int resultCode, Intent data) {            if (requestCode == SELECT_CAMERA) {                Uri selectedImage = imageUri;                getContentResolver().notifyChange(selectedImage, null);                ContentResolver cr = getContentResolver();                try {                    imageSelected = androID.provIDer.MediaStore.Images.Media                     .getBitmap(cr, selectedImage);                    // I trIEd to read the image created for the camera in the Sd                    //But I've got the same error...                     //imageSelected = Utils.readImageSD(this, selectedImage.getPath());                     imageSelected = Utils.rotateImage(Utils.scaleBitmap(imageSelected, 160, 160));                     ivImageLoad.setimageBitmap(imageSelected);                } catch (Exception e) {                    Utils.showToast(getApplicationContext(), R.string.errorLoad);                    Log.e("Camera", e.toString());                }            }}

我希望有人能帮助我…我不知道该怎么办…

提前致谢.

问候.

解决方法:

看起来您只想显示一个160×160的位图,但是在这一行上,您正在读取整个位图,然后将其缩小.

imageSelected = androID.provIDer.MediaStore.Images.Media(cr, selectedImage);

您可以避免使用inJustDecodeBounds将整个位图加载到内存中,然后可以使用inSampleSize对其进行缩放.

这是来自Loading Large Bitmaps Efficiently的AndroID系列的一些示例代码

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWIDth, int reqHeight) {    // Raw height and wIDth of image    final int height = options.outHeight;    final int wIDth = options.outWIDth;    int inSampleSize = 1;    if (height > reqHeight || wIDth > reqWIDth) {        if (wIDth > height) {            inSampleSize = Math.round((float)height / (float)reqHeight);        } else {            inSampleSize = Math.round((float)wIDth / (float)reqWIDth);        }    }    return inSampleSize;}public static Bitmap decodeSampledBitmapFromUri(Uri uri,        int reqWIDth, int reqHeight) {    // First decode with inJustDecodeBounds=true to check dimensions    final BitmapFactory.Options options = new BitmapFactory.Options();    options.inJustDecodeBounds = true;    BitmapFactory.decodeStream(getContentResolver().openinputStream(uri), options);    // Calculate inSampleSize    options.inSampleSize = calculateInSampleSize(options, reqWIDth, reqHeight);    // Decode bitmap with inSampleSize set    options.inJustDecodeBounds = false;    return BitmapFactory.decodeStream(getContentResolver().openinputStream(uri), null, options);}

然后,获取160×160的图片,您只需执行以下 *** 作:

ivImageLoad.setimageBitmap(decodeSampledBitmapFromUri(selectedImage, 100, 100));
总结

以上是内存溢出为你收集整理的在某些设备中拍摄照片android的OutOfMemeryError全部内容,希望文章能够帮你解决在某些设备中拍摄照片android的OutOfMemeryError所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1086165.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存