当我从图库复制图像并保存在外部存储器中时,app很快停止了

时间:2017-01-28 09:52:50

标签: android android-bitmap

我使用下面的代码并多次更改以解决问题,因为我在另一个类中使用相同的代码以及app中的其他更改并且它运行良好。但是在一个类中产生错误。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(requestCode == RESULT && resultCode == RESULT_OK && data != null){
            try {

                File dir, file = null;
                Boolean save = false;
                Bitmap mBitmap;
                FileOutputStream fos;
                ByteArrayOutputStream bao;

                mBitmap = data.getParcelableExtra("data");
                dir = eFunction.getStorageDirectory();

                if(!dir.exists()){
                    if(!dir.mkdirs()){
                        Toast.makeText(ProfilePage.this,
                                "Please manually create a folder with name DS Images in your sd card",
                                Toast.LENGTH_SHORT).show();
                    }
                }

                try {
                    bao = new ByteArrayOutputStream();
                    mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);


                    file = new File(eFunction.getStorageDirectory().toString() , "Profile_Picture.jpeg");
                    if(!file.exists()){
                        save = file.createNewFile();
                    }else{
                        file.delete();
                        save = file.createNewFile();
                    }

                    fos = new FileOutputStream(file);
                    fos.write(bao.toByteArray());
                    fos.close();
                    eFunction.refreshGallery(ProfilePage.this, file);


                }catch (Exception e){
                    try{
                        Toast.makeText(this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
                    }catch (Exception ex){
                        Log.w("DS_142Err", ex);
                    }
                }



                if(save){
                    Toast.makeText(ProfilePage.this, "Profile Picture Successfully Set", Toast.LENGTH_SHORT).show();
                    Profile_pic.setImageBitmap(BitmapFactory.decodeFile(file.toString()));
                }else{

                    Uri ImageUri = data.getData();
                    String[] selectedImage = {MediaStore.Images.Media.DATA};
                    if(ImageUri != null && selectedImage != null) {
                        Cursor mCursor = getContentResolver().query(ImageUri, selectedImage, null, null, null);
                        assert mCursor != null;
                        mCursor.moveToFirst();

                        String picPath = mCursor.getString(mCursor.getColumnIndex(selectedImage[0]));

                        if(picPath != null){

                            mBitmap = BitmapFactory.decodeFile(picPath);
                            bao = new ByteArrayOutputStream();
                            mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bao);

                            file = new File(eFunction.getStorageDirectory().toString(),  "Profile_Picture.jpeg");
                            if(!file.exists()){
                                save = file.createNewFile();
                            }else{
                                file.delete();
                                save = file.createNewFile();
                            }

                            fos = new FileOutputStream(file);
                            fos.write(bao.toByteArray());
                            fos.close();

                            if(save){
                                Toast.makeText(ProfilePage.this, "Profile Picture Successfully Set", Toast.LENGTH_SHORT).show();
                            }else{
                                Toast.makeText(ProfilePage.this, "Error: Profile Picture Not Set", Toast.LENGTH_SHORT).show();
                            }
                            Profile_pic.setImageBitmap(BitmapFactory.decodeFile(file.toString()));
                        }

                    }



                }

            }catch (NullPointerException e){
                Log.w("DS Profile Error", e);
            } catch (FileNotFoundException e) {
                Log.w("DS Profile Error", e);
            } catch (IOException e) {
                Log.w("DS Profile Error", e);
            }catch(Exception e) {
                Log.w("DS Profile Error", e);
            }

        }
    }

但不幸的是,在某些设备中显示以下错误:

    java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://media/external/images/media/255 typ=vnd.android.cursor.dir/image }} to activity {in.mydiscover.discover_branding/in.mydiscover.discover_branding.ProfilePage}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3706)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3749)
    at android.app.ActivityThread.access$1400(ActivityThread.java:153)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1400)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5441)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference
    at in.mydiscover.discover_branding.ProfilePage.onActivityResult(ProfilePage.java:140)
    at android.app.Activity.dispatchActivityResult(Activity.java:6508)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:3702)
    ... 9 more

1 个答案:

答案 0 :(得分:0)

因为此行返回null

mBitmap = data.getParcelableExtra("data");