如何在Uri.parse中使用FileProvider(file://" + filex.toString())& Java.lang.RuntimeException接收BroadCast Intent时出错

时间:2018-01-14 10:35:47

标签: java android android-intent

如何使用Route::post('test', ['as' => 'test', 'uses' => 'MainController@test']); FileProvider中没有收到错误? 我已经尝试了android API >= 24,但它仍然出现错误......
这是我的源代码

FileProvider

首次登录

if(vid.equals("")) {
    inFileName = img.substring(img.lastIndexOf('/') + 1);

    File sdcard = Environment.getExternalStorageDirectory();
    File filex = new File(sdcard, inDir + "/" + "IMAGE_" + inFileName);
    if(filex.exists()) {
        if (inDialog.isShowing())
            inDialog.dismiss();

        Uri uri = Uri.parse("file://" + filex.toString());
        Intent i = new Intent();
        i.setAction(Intent.ACTION_VIEW);
        i.setDataAndType(uri, "image/*");
        inContext.startActivity(i);
    } else {
        inTmp = Uri.parse(img);
        Image_DownloadId = DownloadData(inTmp, "IMAGE_" + inFileName);
    }
}

由于使用android.os.FileUriExposedException: file:///storage/emulated/0/Images/IMAGE_26072028_383759662086144_8624202031520808960_n.jpg exposed beyond app through Intent.getData() 并且需要使用android api >= 24而导致此错误,因此我尝试使用它。 由于使用FileProvider方法,这一行有点令人困惑 toString

这是我想要做的事情

Uri.parse("file://" + filex.toString())

应用程序仍然被强制关闭。

这是日志

if(vid.equals("")) {
inFileName = img.substring(img.lastIndexOf('/') + 1);

File sdcard = Environment.getExternalStorageDirectory();
File filex = new File(sdcard, inDir + "/" + "IMAGE_" + inFileName);
if(filex.exists()) {
    if (inDialog.isShowing())
        inDialog.dismiss();

    Uri uri = FileProvider.getUriForFile(inContext, inContext.getApplicationContext().getPackageName() + ".provider", new File("file://" + filex.toString()));
    Intent i = new Intent(Intent.ACTION_VIEW);
    i.setDataAndType(uri, "image/*");
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    inContext.startActivity(i);
} else {
    inTmp = Uri.parse(img);
    Image_DownloadId = DownloadData(inTmp, "IMAGE_" + inFileName);
}

此行还有其他错误

01-14 17:48:24.288 23382-23382/com.my.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.my.app, PID: 23382
java.lang.IllegalArgumentException: Failed to find configured root that contains /file:/storage/emulated/0/Images/IMAGE_26072028_383759662086144_8624202031520808960_n.jpg
    at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
    at com.my.app.App$Loading$1.onClick(Saver.java:128)
    at android.view.View.performClick(View.java:5619)
    at android.view.View$PerformClick.run(View.java:22295)
    at android.os.Handler.handleCallback(Handler.java:754)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:163)
    at android.app.ActivityThread.main(ActivityThread.java:6321)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)

日志

String filename = cursor.getString(filenameIndex);
BroadcastReceiver downloadReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        //check if the broadcast message is for our Enqueued download
        long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);

        if(referenceId == Image_DownloadId) {

            DownloadManager.Query ImageDownloadQuery = new DownloadManager.Query();
            //set the query filter to our previously Enqueued download
            ImageDownloadQuery.setFilterById(Image_DownloadId);

            //Query the download manager about downloads that have been requested.
            Cursor cursor = downloadManager.query(ImageDownloadQuery);
            if(cursor.moveToFirst()){
                int filenameIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_FILENAME);
                String filename = cursor.getString(filenameIndex);
                Uri uri = Uri.parse("file://" + filename);

                Intent i = new Intent();
                if(inTenTipe.equals("repost")) {
                    i.setAction(Intent.ACTION_SEND);
                    i.putExtra(Intent.EXTRA_STREAM, uri);
                    i.setType("image/*");
                }else {
                    i.setAction(Intent.ACTION_VIEW);
                    i.setDataAndType(uri, "image/*");
                }

                try {
                    if (inDialog.isShowing())
                        inDialog.dismiss();
                    inContext.startActivity(i);
                } catch (android.content.ActivityNotFoundException ex) {
                    File file = new File(filename);
                    boolean deleted = file.delete();
                    Image_DownloadId = DownloadData(inTmp, "IMAGE_" + inFileName);
                }
            }
        }
    }
}

0 个答案:

没有答案