2017-06-19 45 views
0

我下載一個文件,並將其保存爲使用當試圖打開意圖獲取文件「媒體沒有找到」錯誤

File outputDir = context.getCacheDir(); 
File f = File.createTempFile(FILE_TYPE_PREFIX, "." + extension,outputDir); 

f.exists一個臨時文件()說,該文件存在,並以及我設置f.setReadable(true,false);能夠讀取它。

的我開始了新的意向

Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(Uri.fromFile(f), mimeType);    
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(intent); 

殲返回

/data/user/0/com.app.name/cache/PP_278545395.png 

和Uri.fromFile()後

file:///data/user/0/com.app.name/cache/PP_278545395.png 

當我嘗試打開該文件,它打開圖庫,但告訴我「找不到媒體」。

任何有關此問題的原因存在的想法?

回答

1

getCacheDir()是您的應用程序的internal storage的一部分。其他應用程序無法訪問它。使用FileProvider將該內容發佈到其他應用程序。

+0

請您詳細介紹一下。我對Android/Java很陌生。 – iremlopsum

+0

@iremlopsum:嗯,這個問題與[博客文章解釋了Android SDK的內部存儲意味着什麼](https://commonsware.com/blog/2014/04/07/storage-situation-internal-storage的.html)。 [這是一篇博客文章](https://commonsware.com/blog/2016/03/16/how-publish-files-via-content-uri.html)關於通過「內容」Uri發佈文件,這是[FileProvider的功能](https://developer.android.com/reference/android/support/v4/content/FileProvider.html)。 – CommonsWare

+0

@iremlopsum:另外,[這裏是一個示例應用程序](https://github.com/commonsguy/cw-omnibus/tree/master/ContentProvider/V4FileProvider),它使用'FileProvider'(服務從'assets/',所以樣本可以自舉)。 – CommonsWare

相關問題