2017-06-15 148 views
1
.TXT文件在文件浏览器

用户点击获取对话框的“打开方式...”并没有列出我的应用程序。 当我尝试打开文件时,我想获取它的绝对路径。我得到的是内容:// URI如何使用intent.action.VIEW打开文件时获取文件URI?

I/ActivityManager: START u0 {act=android.intent.action.VIEW dat=content://com.android.providers.downloads.documents/document/1031 typ=text/plain

如果我想要检索路径,我的文件,我需要得到的file:// URI。我怎样才能得到file:// URI而不是content://

这里是AndroidManifest

 <intent-filter > 
      <action android:name="android.intent.action.VIEW" /> 
      <category android:name="android.intent.category.DEFAULT" />  
      <data android:mimeType="text/*"/> 
     </intent-filter> 

这里是我尝试处理文件:

Intent i = getIntent(); 
String action = i.getAction(); 
String type = i.getType(); 

if (Intent.ACTION_VIEW.equals(action) && type != null) { 
    Uri uri = i.getData(); 
    if (uri != null) { 
     Toast.makeText(this, uri.toString(), Toast.LENGTH_SHORT).show(); 
    }     
} 

回答

1

这里是AndroidManifest

<intent-filter>是说,你可以处理text/*内容不管它来自

当我尝试打开文件,我想它的绝对路径

然后你需要添加<data android:scheme="file"><intent-filter>,声明自己只与file方案工作。您的应用程序将不再出现在应用程序的选项,不使用fileUri值,如“文件管理器”您正在使用。而且,由于fileUri值在Android 7.0+上被有效禁用,因此随着时间的推移,您的应用将不再有用。到2020年左右,你的应用将无用武之地。

或者,你可以摆脱的“希望得到它的绝对路径”的要求。如果你想要从一个filecontentUri文本内容,您可以在ContentResolver使用openInputStream(),例如。

+0

但我想不是只读文件,我也想编辑文件并保存。 –

+0

@PersonalJesus:在'ContentResolver'上也有'openOutputStream()'。督察,你会做一个绝对路径和同样的事情'FileInputStream' /'FileOutputStream'的东西,你可以用'content'做'Uri'和'openInputStream()'/'openOutputStream()'。 – CommonsWare