在OutOfMemoryError异常S3星系星系、异常、OutOfMemoryError

由网友(专一的男人)分享简介:意图localIntent =新意图(                android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);        文件DIR =环境                .getExternalStoragePublicDirectory(Envi...

 意图localIntent =新意图(                android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);        文件DIR =环境                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);        随机=新的随机();        SEQNO = random.nextInt(1000000);        photoName =将String.valueOf(SEQNO);        输出=新的文件(目录,photoName +.JPEG);        imgPath = Uri.fromFile(输出);        localIntent.putExtra(MediaStore.EXTRA_OUTPUT,imgPath);        PATH = output.getAbsolutePath();        startActivityForResult(localIntent,PICK_Camera_IMAGE);保护无效的onActivityResult(INT申请code,INT结果code,意图数据){        super.onActivityResult(要求code,结果code,数据);            如果(要求code == PICK_Camera_IMAGE&放大器;&安培;结果code == RESULT_OK){                        尝试{                            文件f =新的文件(路径);                            矩阵mat =新的Matrix();                            mat.postRotate(90);//                            BMP位图= BitmapFactory.de codeStream(新的FileInputStream(F),NULL,NULL);                            位图bmpPic = Bitmap.createBitmap(BMP,0,0,bmp.getWidth(),bmp.getHeight(),凉席,真); 

请注意:我用这个code使用相机拍照。它运行在除了三星Galaxy S3每一个设备。当我在S3上运行,它给错误,如OutOfMemoryError错误。

LogCat中:

  11月4日至27日:26:49.775:E / AndroidRuntime(6356):java.lang.OutOfMemoryError十一月4日至27日:26:49.775:E / AndroidRuntime(6356):在android.graphics.Bitmap.nativeCreate(本机方法)十一月4日至27日:26:49.775:E / AndroidRuntime(6356):在android.graphics.Bitmap.createBitmap(Bitmap.java:640)十一月4日至27日:26:49.775:E / AndroidRuntime(6356):在android.graphics.Bitmap.createBitmap(Bitmap.java:586)十一月4日至27日:26:49.775:E / AndroidRuntime(6356):在com.example.camera.CameraAct $ 10 $ 1.run(CameraAct.java:378)十一月4日至27日:26:49.775:E / AndroidRuntime(6356):在android.os.Handler.handleCallback(Handler.java:615)十一月4日至27日:26:49.775:E / AndroidRuntime(6356):在android.os.Handler.dispatchMessage(Handler.java:92)十一月4日至27日:26:49.775:E / AndroidRuntime(6356):在android.os.Looper.loop(Looper.java:137)十一月4日至27日:26:49.775:E / AndroidRuntime(6356):在android.app.ActivityThread.main(ActivityThread.java:4898)十一月4日至27日:26:49.775:E / AndroidRuntime(6356):在java.lang.reflect.Method.invokeNative(本机方法)十一月4日至27日:26:49.775:E / AndroidRuntime(6356):在java.lang.reflect.Method.invoke(Method.java:511)十一月4日至27日:26:49.775:E / AndroidRuntime(6356):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1006) 

解决方案 OutOfMemoryError异常

您需要使用它之前减少位图的样本大小。这可用于这样做。

 私人位图德codeFILE(档案文件){    尝试    {        // *********************德code图像尺寸********************        BitmapFactory.Options选项=新BitmapFactory.Options();        options.inJustDe codeBounds = TRUE;        BitmapFactory.de codeStream(新的FileInputStream(文件),空,期权);        // **********************找到正确的比例值。它应该是2的幂********************        options.inSampleSize = BitmapConverter.calculateInSampleSize(选件,145,105);        options.inJustDe codeBounds = FALSE;        返回BitmapFactory.de codeStream(新的FileInputStream(文件),空,期权);    }    赶上(FileNotFoundException异常eFileNotFoundException)    {        返回null;    }} 

然后你就可以调用如果需要Bitmap.recycle(),但我觉得这不会是必要的。

Intent localIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File dir = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
        random = new Random();
        seqNo = random.nextInt(1000000);
        photoName = String.valueOf(seqNo);
        output = new File(dir, photoName + ".jpeg");
        imgPath = Uri.fromFile(output);
        localIntent.putExtra(MediaStore.EXTRA_OUTPUT, imgPath);
        path = output.getAbsolutePath();
        startActivityForResult(localIntent, PICK_Camera_IMAGE);


protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
            if (requestCode== PICK_Camera_IMAGE && resultCode == RESULT_OK)  {


                        try {


                            File f = new File(path);

                            Matrix mat = new Matrix();
                            mat.postRotate(90);
//
                            Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f), null, null);
                            Bitmap bmpPic = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);  

Note: i Used this code for taking picture using camera. it runs in every devices except samsung galaxy s3. when i run on s3, it gives error like OutOfMemoryError.

LogCat:

04-27 11:26:49.775: E/AndroidRuntime(6356): java.lang.OutOfMemoryError
04-27 11:26:49.775: E/AndroidRuntime(6356):     at android.graphics.Bitmap.nativeCreate(Native Method)
04-27 11:26:49.775: E/AndroidRuntime(6356):     at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
04-27 11:26:49.775: E/AndroidRuntime(6356):     at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
04-27 11:26:49.775: E/AndroidRuntime(6356):     at com.example.camera.CameraAct$10$1.run(CameraAct.java:378)
04-27 11:26:49.775: E/AndroidRuntime(6356):     at android.os.Handler.handleCallback(Handler.java:615)
04-27 11:26:49.775: E/AndroidRuntime(6356):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 11:26:49.775: E/AndroidRuntime(6356):     at android.os.Looper.loop(Looper.java:137)
04-27 11:26:49.775: E/AndroidRuntime(6356):     at android.app.ActivityThread.main(ActivityThread.java:4898)
04-27 11:26:49.775: E/AndroidRuntime(6356):     at java.lang.reflect.Method.invokeNative(Native Method)
04-27 11:26:49.775: E/AndroidRuntime(6356):     at java.lang.reflect.Method.invoke(Method.java:511)
04-27 11:26:49.775: E/AndroidRuntime(6356):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)

解决方案

You need to reduce the sample size of the bitmap before using it. This can be used to do so.

private Bitmap decodeFile(File file)
{
    try 
    {
        //********************* decode image size ********************
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(file), null, options);

        // ********************** Find the correct scale value. It should be the power of 2. ********************
        options.inSampleSize = BitmapConverter.calculateInSampleSize(options, 145, 105);
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeStream(new FileInputStream(file), null, options);
    } 
    catch(FileNotFoundException eFileNotFoundException) 
    {

        return null;
    }
}

And then you can call Bitmap.recycle() if required, although I feel that it wouldn't be necessary.

阅读全文

相关推荐

最新文章