简体   繁体   English

星系S3中的OutOfMemoryError

[英]OutOfMemoryError in galaxy s3

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. 注意:i该代码用于使用相机拍照。 it runs in every devices except samsung galaxy s3. 它可以在除三星银河s3之外的所有设备中运行。 when i run on s3, it gives error like OutOfMemoryError. 当我在s3上运行时,它给出了类似OutOfMemoryError的错误。

LogCat: 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. 然后,如果需要,您可以调用Bitmap.recycle(),尽管我认为这不是必需的。

BitmapFactory.Options options = new BitmapFactory.Options();
// will results in a much smaller image than the original
options.inSampleSize = 8;
Bitmap bitmap = BitmapFactory.decodeFile(PATH,options);

Also take a look at this answer that may help you. 也看看这个答案可以帮助您。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM