Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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
Android 向应用程序共享文件-表示FileNotFoundException_Android_File_Filenotfoundexception - Fatal编程技术网

Android 向应用程序共享文件-表示FileNotFoundException

Android 向应用程序共享文件-表示FileNotFoundException,android,file,filenotfoundexception,Android,File,Filenotfoundexception,我尝试将下面代码为的图像共享到Slack应用程序,但它显示“共享文件时出错。请重试”。请注意,我已设置sharingint.setType(“image/*”),并且正在从缓存中提取图像。仍然无法指出错误。感谢您的指导 代码: try { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss"); String currentDate

我尝试将下面代码为的图像共享到Slack应用程序,但它显示“共享文件时出错。请重试”。请注意,我已设置sharingint.setType(“image/*”),并且正在从缓存中提取图像。仍然无法指出错误。感谢您的指导

代码:

try {
                        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
                        String currentDateAndTime = sdf.format(new Date());
                        filename = "Epic_" + currentDateAndTime + ".jpg";
                        File file = new File(context.getCacheDir(), filename +  ".png");
                        FileOutputStream fos = new FileOutputStream(file);
                        bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
                        fos.flush();
                        fos.close();
                        file.setReadable(true, false);
                        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
                        sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                        sharingIntent.setType("image/*");
                        context.startActivity(Intent.createChooser(sharingIntent, "Share Via"));
                        subscriber.onCompleted();
                    } catch (Exception e) {
                    }
日志:

E/null: java.io.FileNotFoundException: Attempting to access private files
                                         java.lang.IllegalArgumentException: java.io.FileNotFoundException: Attempting to access private files
                                             at com.Slack.persistence.fileupload.FilePartialUploadJob$Builder.uriFile(FilePartialUploadJob.java:107)
                                             at com.Slack.ui.UploadActivity$34.call(UploadActivity.java:1253)
                                             at com.Slack.ui.UploadActivity$34.call(UploadActivity.java:1240)
                                             at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46)
                                             at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35)
                                             at rx.Observable.unsafeSubscribe(Observable.java:9861)
                                             at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94)
                                             at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:221)
                                             at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                                             at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
                                             at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                             at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
                                             at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                             at java.lang.Thread.run(Thread.java:818)
                                          Caused by: java.io.FileNotFoundException: Attempting to access private files
                                             at com.Slack.persistence.fileupload.FilePartialUploadJob$Builder.uriFile(FilePartialUploadJob.java:107) 
                                             at com.Slack.ui.UploadActivity$34.call(UploadActivity.java:1253) 
                                             at com.Slack.ui.UploadActivity$34.call(UploadActivity.java:1240) 
                                             at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:46) 
                                             at rx.internal.operators.OnSubscribeDefer.call(OnSubscribeDefer.java:35) 
                                             at rx.Observable.unsafeSubscribe(Observable.java:9861) 
                                             at rx.internal.operators.OperatorSubscribeOn$1.call(OperatorSubscribeOn.java:94) 
                                             at rx.internal.schedulers.CachedThreadScheduler$EventLoopWorker$1.call(CachedThreadScheduler.java:221) 
                                             at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55) 
                                             at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422) 
                                             at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                             at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152) 
                                             at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265) 
                                             at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                             at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                             at java.lang.Thread.run(Thread.java:818) 
getCacheDir()
仅为应用程序提供私有内部内存。因此其他应用程序无法访问


您发布的日志似乎来自不同的应用程序,因为它清楚地表明无法访问此类私人文件。

好的。所以你建议使用另一个公共缓存目录来存储,对吗?不,不,不,不,不,不,不。不需要。但不是私人的。比如
getExternalCacheDir()
。其他应用程序也可以访问那里。如果他们请求读取外部存储权限。拿到了。看来我还得再检查一下文件。非常感谢你的帮助。:)