文件共享到应用程序 - 说FileNotFoundException

时间:2017-01-25 06:36:48

标签: android file filenotfoundexception

我尝试使用以下代码与Slack应用程序共享图像,但它显示“错误共享文件。请再试一次。”请注意,我已设置sharingIntent.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) {
                    }

logcat的:

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) 

1 个答案:

答案 0 :(得分:0)

getCacheDir()仅为您的应用提供私人内存。所以其他应用程序无权访问。

您发布的logcat看起来好像是来自不同的应用程序,因为它清楚地表明此类私有文件无法访问。