'失败交付结果ResultInfo'然后在崩溃后正常工作

时间:2017-04-17 21:18:19

标签: java android exception android-fileprovider android-7.0-nougat

我不得不更新我的应用程序以与新的Android版本一起使用,针对N版本,我更新了:

从:

Uri.fromFile();

为:

FileProvider.getUriForFile();

我这样做了如下:

基于:(android.os.FileUriExposedException: file:///storage/emulated/0/test.txt exposed beyond app through Intent.getData()

<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities=" com.mydomain.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/provider_paths"/>
</provider>

provider_paths:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-cache-path name="external_files" path="."/>
</paths>

FntTakePicture:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState)

//......
//......

btnTakePicture.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        if (takePictureIntent.resolveActivity(getContext().getPackageManager()) != null) {


            outputFileName = null;
            try {

                /*if(fragmentPosition == 0 || fragmentPosition == 4)
                    outputFileName = Images.createImageFile2();//PNG
                else
                    outputFileName = Images.createImageFile3();//JPG*/

                outputFileName = Images.createImageFile3();

                //takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(outputFileName));
                Uri photoURI = FileProvider.getUriForFile(getContext(),"com.mydomain.fileprovider",outputFileName);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,photoURI);
                takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

                startActivityForResult(takePictureIntent, RC_IMAGE_CAPTURE);

            } catch (IOException e) {
                e.printStackTrace();
                FirebaseCrash.report(e);
            }

        }
    }
});

}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == RC_IMAGE_CAPTURE && resultCode == RESULT_OK) {

        loadImage(outputFileName.getAbsolutePath());//outputFileName.getAbsolutePath()

        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
            txtInstructions.setText(Html.fromHtml(
                    String.format(
                            getResources().getString(R.string.validate_picture2),
                            defaultValidateText
                    ),Html.FROM_HTML_MODE_LEGACY
            ));

        } else {
            txtInstructions.setText(Html.fromHtml(
                    String.format(
                            getResources().getString(R.string.validate_picture2),
                            defaultValidateText
                    )), TextView.BufferType.SPANNABLE);
        }

        btnTakePicture.setText(getString(R.string.take_photo_again));
        //this.data.setImage(Images.compressImage(picture),fragmentPosition);
        this.data.setImage(outputFileName.getAbsolutePath(),fragmentPosition);
        Log.e("Data Picture"," Vin: "+this.data.getVin()+" lon: "+this.data.getLon()+ " lat: "+this.data.getLat());


        if(lastPage)
        validateContinueBoton();
        listener.save(this.data);
    }
}

public void loadImage(final String path){
    new AsyncTask<Void,Void,Bitmap>(){

        @Override
        protected Bitmap doInBackground(Void... params) {

            return Images.decodeSampledBitmapFromFile(path,
                    getResources().getDimensionPixelSize(R.dimen.ancho_default)/2);
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            imgPictureTaken.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imgPictureTaken.setImageBitmap(bitmap);
        }
    }.execute(null,null,null);
}

图片类:

public class Images{


    static File createImageFile3() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",Locale.US).format(new Date());
        String imageFileName = "NTS_Image_" + timeStamp + "_";
        File storageDir = ContextCompat.getExternalCacheDirs(App.getContext())[0];

        File nf = File.createTempFile(
                imageFileName,  // prefix
                ".jpg",         // suffix
                storageDir      // directory
        );

        nf.deleteOnExit();
        return nf;
    }
}

此外,这个事件是一个片段,我不知道这是否重要。

运行安装应用程序后,给它权限,然后一旦正常工作就崩溃......

我得到了这个例外:

04-17 15:00:10.058 669-669 /? E / cnss-daemon:陈旧或无法访问的邻居,ndm状态:4 04-17 15:00:10.202 13596-13596 / com.mydomain E / AndroidRuntime:FATAL EXCEPTION:main                                                                        进程:com.mydomain,PID:13596                                                                        java.lang.RuntimeException:将结果ResultInfo {who = null,request = 74538,result = -1,data = Intent {act = inline-data(has extras)}}传递给activity {com.mydomain / com.mydomain。 CaptureWizard}:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String java.io.File.getAbsolutePath()'                                                                            在android.app.ActivityThread.deliverResults(ActivityThread.java:4094)                                                                            在android.app.ActivityThread.handleSendResult(ActivityThread.java:4137)                                                                            在android.app.ActivityThread.-wrap20(ActivityThread.java)                                                                            在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1529)                                                                            在android.os.Handler.dispatchMessage(Handler.java:102)                                                                            在android.os.Looper.loop(Looper.java:154)                                                                            在android.app.ActivityThread.main(ActivityThread.java:6123)                                                                            at java.lang.reflect.Method.invoke(Native Method)                                                                            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)                                                                         引发者:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'java.lang.String java.io.File.getAbsolutePath()'                                                                            在com.mydomain.FntTakePicture.onActivityResult(FntTakePicture.java:201)                                                                            在android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:164)                                                                            在com.mydomain.CaptureWizard.onActivityResult(CaptureWizard.java:133)                                                                            在android.app.Activity.dispatchActivityResult(Activity.java:6931)                                                                            在android.app.ActivityThread.deliverResults(ActivityThread.java:4090)                                                                            在android.app.ActivityThread.handleSendResult(ActivityThread.java:4137)                                                                            在android.app.ActivityThread.-wrap20(ActivityThread.java)                                                                            在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1529)                                                                            在android.os.Handler.dispatchMessage(Handler.java:102)                                                                            在android.os.Looper.loop(Looper.java:154)                                                                            在android.app.ActivityThread.main(ActivityThread.java:6123)                                                                            at java.lang.reflect.Method.invoke(Native Method)                                                                            在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:867)                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)

1 个答案:

答案 0 :(得分:0)

确保将<ion-grid> <div *ngFor="let button of pillars; let i = index;"> <ion-row *ngIf="button.ref == 'pillar'"> <ion-col> <btn-rounded-picture [text]="button.name" [imageURL]="'/assets/assess/' + button.name + '.png'" (click)="detailPillar(button)"></btn-rounded-picture> </ion-col> </ion-row> </div> </ion-grid> 保存在已保存的实例状态outputFileName中。当您的应用处于后台时,您的流程可能会被终止,这会导致该字段在新流程中默认为Bundle