2016-06-18 64 views
1

之前Android N我打開它想:下載文件後,在我的應用程序中打開的Android N A下載的文件

Intent myIntent = new Intent(Intent.ACTION_VIEW); 
File myFile = new File(result); 
String mime = URLConnection.guessContentTypeFromStream(new FileInputStream(myFile)); 
myIntent.setDataAndType(Uri.fromFile(myFile), mime); 
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(myIntent); 

由於Android N如果我使用此代碼我得到一個FileUriExposedException,作爲建議here我應該使用FileProvider並得到這樣的路徑:

Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile); 

,然後打開它:

ParcelFileDescriptor.openFile(Uri uri,String mode) 

的建議here在那裏說Uri: A content URI associated with a file, as returned by getUriForFile().

但是在IDE如果我做ParcelfileDescriptor.open...沒有方法openFile() Image

open()用一個文件作爲參數,並沒有URI。那麼如何在Android N中打開文件?

注意:我不想用我的應用程序打開文件。例如,如果我下載PDF,我想用手機上已安裝的應用程序打開它。

+0

[檢查這個(http://stackoverflow.com/a/41570845/6333971) –

回答

0

由於Android否如果我用這個代碼,我得到一個FileUriExposedException,這裏建議我應該使用FileProvider

那部分是正確的。您從getUriForFile()獲得的Uri然後進入您的ACTION_VIEWIntent。所以你風的東西,如:

File myFile = new File(result); 
Uri uri = FileProvider.getUriForFile(context, YOUR_AUTHORITY, myFile); 
Intent myIntent = new Intent(Intent.ACTION_VIEW, uri); 
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
context.startActivity(myIntent); 
+0

我用這個代碼,但它顯示錯誤不能播放視頻... Intent intent = new Intent(); intent.setAction(Intent.ACTION_VIEW); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.setDataAndType(intentUri,「video/*」); startActivity(intent); –

+0

@bdevloper:與視頻播放器應用程序的開發人員交談。 – CommonsWare

+0

但是,在7.0以下工作正常。 –

相關問題