Android 当我从gallery复制图像并保存到外部存储时,应用程序不幸停止

Android 当我从gallery复制图像并保存到外部存储时,应用程序不幸停止,android,android-bitmap,Android,Android Bitmap,我正在使用下面的代码并多次更改它以解决问题,因为我在其他类中使用的代码和在应用程序中进行的其他更改的代码都是相同的,并且工作得非常好。但在一个类中会产生错误 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); i

我正在使用下面的代码并多次更改它以解决问题,因为我在其他类中使用的代码和在应用程序中进行的其他更改的代码都是相同的,并且工作得非常好。但在一个类中会产生错误

    @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

因为这行返回null

mBitmap = data.getParcelableExtra("data");

因为这行返回null

mBitmap = data.getParcelableExtra("data");

我想知道为什么data.getParcelableExtra(“数据”)返回null?在使用mBitmap之前,请检查它是否为null。然后不要继续代码,而是停止。向这样说的用户敬酒。如果您这样做,您的应用程序不会崩溃。当然,你的应用程序不应该崩溃。你还应该在所有的catch块中放置祝酒词,以显示e.getMessage()。否则,应用程序的用户不知道会发生什么。您也可以。@greenapps-OkayI我想知道为什么data.getParcelableExtra(“数据”)返回null?在使用mBitmap之前,请检查它是否为null。然后不要继续代码,而是停止。向这样说的用户敬酒。如果您这样做,您的应用程序不会崩溃。当然,你的应用程序不应该崩溃。你还应该在所有的catch块中放置祝酒词,以显示e.getMessage()。否则,应用程序的用户不知道会发生什么。你也可以。@greenapps-ok
mBitmap=data.getParcelableExtra(“数据”)。从未见过这样提取位图的方法。您可以显示启动选择操作的已用意图吗?@greenapps-我使用了以下意图
intent intent intent=new intent(intent.action\u PICK,MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI);意图。额外(“作物”、“真实”);意向。额外(“输出”,300);意向。额外投入(“产出”,300);intent.putExtra(“aspectX”,1);意图.putExtra(“aspectY”,1);intent.putExtra(“返回数据”,true);startActivityForResult(意图、结果)
mBitmap=data.getParcelableExtra(“数据”)。从未见过这样提取位图的方法。您可以显示启动选择操作的已用意图吗?@greenapps-我使用了以下意图
intent intent intent=new intent(intent.action\u PICK,MediaStore.Images.Media.EXTERNAL\u CONTENT\u URI);意图。额外(“作物”、“真实”);意向。额外(“输出”,300);意向。额外投入(“产出”,300);intent.putExtra(“aspectX”,1);意图.putExtra(“aspectY”,1);intent.putExtra(“返回数据”,true);startActivityForResult(意图、结果)