Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android.os.FileUrieExposedException:在android.os.StrictMode.OnFileUrieExposed_Java_Android_Android Fileprovider - Fatal编程技术网

Java android.os.FileUrieExposedException:在android.os.StrictMode.OnFileUrieExposed

Java android.os.FileUrieExposedException:在android.os.StrictMode.OnFileUrieExposed,java,android,android-fileprovider,Java,Android,Android Fileprovider,下面我将介绍如何使用FileProvider而不是Uri.FromFile(),但我不确定如何在我的服务中使用FileProvider FileProvider.getUriForFile()要求第一个参数有一个上下文,但因为我在服务中使用它,所以我没有活动上下文 我该怎么办 我需要改变这一点: Uri swatchImageUri = Uri.fromFile(favoritesImageFile); 按照他们上面的建议 以下是我在不使用FileProvider的情况下得到的当前异常: 20

下面我将介绍如何使用FileProvider而不是
Uri.FromFile()
,但我不确定如何在我的服务中使用FileProvider

FileProvider.getUriForFile()
要求第一个参数有一个上下文,但因为我在服务中使用它,所以我没有活动上下文

我该怎么办

我需要改变这一点:

Uri swatchImageUri = Uri.fromFile(favoritesImageFile);
按照他们上面的建议

以下是我在不使用FileProvider的情况下得到的当前异常:

2019-07-23 23:04:42.748 11034-11034/com.company.projname E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.company.projname, PID: 11034
    android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.company.projname/cache/colors.png exposed beyond app through ClipData.Item.getUri()
        at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
        at android.net.Uri.checkFileUriExposed(Uri.java:2371)
        at android.content.ClipData.prepareToLeaveProcess(ClipData.java:963)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10216)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10222)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10201)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1667)
        at android.app.Activity.startActivityForResult(Activity.java:4586)
代码段:

public void shareColorSwatch(SwatchViewModel model){

        try {
            String swatchName = model.First.get().Name.get().replaceAll("\\s","").replaceAll("'", "") + "_" +
                    model.Second.get().Name.get().replaceAll("\\s","").replaceAll("'", "") + "_" +
                    model.Third.get().Name.get().replaceAll("\\s","").replaceAll("'", "");


            Display display = activity.getWindowManager().getDefaultDisplay();
            int screenWidth =  display.getWidth();
            int screenHeight = display.getHeight();

            Bitmap swatchBitmap = createColorSwatchBitmap(model, screenWidth, screenHeight);

            String fileName = String.format("swatch_%s.png", swatchName);
            File swatchImageFile = getImageFile(activity,fileName, swatchBitmap, true);
            Uri swatchImageUri = FileProvider.getUriForFile(//,
                    BuildConfig.APPLICATION_ID + ".provider",
                    swatchImageFile);
            //Uri swatchImageUri = Uri.fromFile(swatchImageFile);
            shareImage(activity,swatchName,swatchImageUri);
        }catch (Exception e)
        {
            e.printStackTrace();
        }

    }
我该怎么办

服务
是一个
上下文
。因此,如果您在
服务中直接调用
getUriForFile()
,请在
上下文中使用
this
行中:

 Uri swatchImageUri = FileProvider.getUriForFile(//,
                    BuildConfig.APPLICATION_ID + ".provider",
                    swatchImageFile);
将活动作为第一个参数传递:

 Uri swatchImageUri = FileProvider.getUriForFile(activity,
                BuildConfig.APPLICATION_ID + ".provider",
                swatchImageFile);

我试图这样做,但它不允许我使用:错误的第一个参数类型。找到:'com.company.projname.service.ShareService',必需:'android.content.Context'@Euridice01:Then
ShareService
不是从
service
扩展而来的。不,不是。你是对的。它只是一个服务类,但不是从服务扩展而来。我现在能做什么?@Euridice01:您似乎正在使用
startActivityForResult()
创建此
意图
,因此您必须有一个
活动
片段
进行调用。那么,从那里获取你的
上下文
。你能将你的服务代码粘贴到调用方法的地方吗?@juandianielornella发布。哦,我只是在想它应该只是上面的活动?