简体   繁体   中英

Null pointer exception when compressing image

When on Samsung Galaxy S3 I select image from gallery, ocasionaly I recieve null pointer exception on compressing stage.

problem line

bm.compress(CompressFormat.JPEG, 85, bos);

method

public String saveFile(Bitmap bm, int id) {
        String file_path = Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/Mk";
        File dir = new File(file_path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File file = new File(dir, "smaller" + id + ".jpeg");
        FileOutputStream fOut;
        try {
            fOut = new FileOutputStream(file);
            // bm.compress(Bitmap.CompressFormat.PNG, 85, fOut);
            BufferedOutputStream bos = new BufferedOutputStream(fOut);
            bm.compress(CompressFormat.JPEG, 85, bos);
            if (bos != null) {
                bos.flush();
                bos.close();
            }
            if (fOut != null) {
                fOut.flush();
                fOut.close();
            }
            if (id == 1) {
                photo1 = file.getPath();
            } else if (id == 2) {
                photo2 = file.getPath();
            } else if (id == 3) {
                photo3 = file.getPath();
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return "";

    }

Error

08-09 10:31:06.740: E/AndroidRuntime(16358): FATAL EXCEPTION: main
08-09 10:31:06.740: E/AndroidRuntime(16358): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=11, result=-1, data=Intent { dat=content://com.sec.android.gallery3d.provider/picasa/item/5813366421292566690 (has extras) }} to activity {ua.mirkvartir.android.frontend/ua.mirkvartir.android.frontend.AddFillActivityApp}: java.lang.NullPointerException
08-09 10:31:06.740: E/AndroidRuntime(16358):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3182)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3225)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at android.app.ActivityThread.access$1100(ActivityThread.java:140)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1275)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at android.os.Looper.loop(Looper.java:137)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at android.app.ActivityThread.main(ActivityThread.java:4898)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at java.lang.reflect.Method.invokeNative(Native Method)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at java.lang.reflect.Method.invoke(Method.java:511)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at dalvik.system.NativeStart.main(Native Method)
08-09 10:31:06.740: E/AndroidRuntime(16358): Caused by: java.lang.NullPointerException
08-09 10:31:06.740: E/AndroidRuntime(16358):    at ua.mirkvartir.android.frontend.AddFillActivityApp.saveFile(AddFillActivityApp.java:856)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at ua.mirkvartir.android.frontend.AddFillActivityApp.onActivityResult(AddFillActivityApp.java:801)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at android.app.Activity.dispatchActivityResult(Activity.java:5390)
08-09 10:31:06.740: E/AndroidRuntime(16358):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3178)
08-09 10:31:06.740: E/AndroidRuntime(16358):    ... 11 more

onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        String a = "";
        if (data == null) {
            a = "null";
        } else
            a = data.toString();
        Log.d("RealPAth", "resultCode " + resultCode + " data " + a);
        if (requestCode == 12 && resultCode == RESULT_OK) {


        } else if (requestCode == 11 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String picturePath = getRealPathFromURI(selectedImage);

            pho1.setImageBitmap(decodeSampledBitmapFromResource(picturePath,
                    80, 60));
            saveFile(decodeSampledBitmapFromResource(picturePath, 800, 800), 1);

        } else if (requestCode == 22 && resultCode == RESULT_OK) {

        } else if (requestCode == 21 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String picturePath = getRealPathFromURI(selectedImage);
            pho2.setImageBitmap(decodeSampledBitmapFromResource(picturePath,
                    80, 60));
            saveFile(decodeSampledBitmapFromResource(picturePath, 800, 800), 2);

        } else if (requestCode == 32 && resultCode == RESULT_OK) {

        } else if (requestCode == 31 && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();

            String picturePath = getRealPathFromURI(selectedImage);
            pho3.setImageBitmap(decodeSampledBitmapFromResource(picturePath,
                    80, 60));

            saveFile(decodeSampledBitmapFromResource(picturePath, 800, 800), 3);
        }
    }

decodeSampleBitmapFromResourse

public static Bitmap decodeSampledBitmapFromResource(String path,
            int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSizeа
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }

Your variable bm is null . As it's reached in as parameter, you'll either have to debug/log there or make your saveFile method waterproof by testing and handling bm for null.

I think there is problem with the path of your File Path
You are missing a slash after the Directory name

String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Mk"+"/";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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