CHo*_*n82 1 android android-intent
我正在开发一个使用自定义文件类型的应用程序,我已经找到了如何使用系统注册我的应用程序,因为能够使用intent过滤器打开此类型的文件,我的应用程序也将显示在应用程序列表中在尝试打开此类附件时,可用于从内置电子邮件客户端打开此文件.问题是从文件浏览器传入时处理打开文件的代码在从电子邮件客户端传入时不起作用.在选择正确类型的文件后,从文件浏览器调用Activity时,我使用的代码正常工作:
Intent i = getIntent();
if(i == null) return;
Uri u = i.getData();
if(u == null) return;
String filePath = u.getEncodedPath();
if(filePath == null) return;
Util.loadOTDRFile(filePath);
Run Code Online (Sandbox Code Playgroud)
从文件浏览器加载时,我在"filePath"字符串中获得的内容类似于"mnt/storage/Android/data/com.fiberdroid.001/downloads/filename.trc ...并且工作正常我的应用程序加载它loadOTDRFile()函数成功完成.
但是,当我从电子邮件客户端打开相同类型的文件时,该代码中的filePath变量最终会出现如下情况:"// mail/parts/4217"无法加载,我的加载函数返回找不到的文件错误.
以下是loadOTDRFile()函数的相关代码:
File file = new File(filePath);
InputStream is;
try
{
is = new FileInputStream(filePath);
}
catch(FileNotFoundException e)
{
return D.ERROR_FILENOTFOUND;
}
Run Code Online (Sandbox Code Playgroud)
我想我的问题是什么样的路径是"// mail/parts/4217",为什么我不能打开它?
谢谢.
这是一个内容URI ...您需要使用ContentResolver从邮件提供商处打开该文件.
要做到这一点,你应该这样做:
getContentResolver().openInputStream(u); // Where 'u' is the uri you extract from the intent.
Run Code Online (Sandbox Code Playgroud)
你可能想要这样的东西:
Uri u = i.getData();
String scheme = u.getScheme();
if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
// handle as content uri
} else {
// handle as file uri
}
Run Code Online (Sandbox Code Playgroud)
对于任何可能偶然发现这个问题的人来说,这就是我从内容URI或从第三方应用程序传入意图传入的文件URI中读取文件数据的方法:
首先,将这些意图过滤器放在清单中,将".trc"替换为您希望应用程序打开的类型的文件扩展名:
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
<data android:scheme="file" />
<data android:host="*" />
<data android:port="*" />
<data android:pathPattern=".*..*..*..*..*..*.trc" />
<data android:pathPattern=".*..*..*..*..*.trc" />
<data android:pathPattern=".*..*..*..*.trc" />
<data android:pathPattern=".*..*..*.trc" />
<data android:pathPattern=".*..*.trc" />
<data android:pathPattern=".*.trc" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:scheme="content" />
<data android:mimeType="text/plain" />
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
不要问我这些路径模式,我尝试了许多在本网站上遇到该解决方案之前无效的事情,这是第一个有效的,所以我保留了它.
然后,将类似于此的代码添加到将接收文件的活动的onCreate()方法(util.loadOTDRFile函数特定于我的应用程序,您必须自己加载文件):
Intent i = getIntent();
if(i == null) return;
Uri u = i.getData();
if(u == null) return;
String scheme = u.getScheme();
if(ContentResolver.SCHEME_CONTENT.equals(scheme))
{
try
{
ContentResolver cr = getContentResolver();
AssetFileDescriptor afd = cr.openAssetFileDescriptor(u, "r");
long length = afd.getLength();
byte[] filedata = new byte[(int) length];
InputStream is = cr.openInputStream(u);
if(is == null) return;
try
{
is.read(filedata, 0, (int) length);
Util.loadOTDRFileFromByteArray(filedata);
}
catch(IOException e)
{
return;
}
}
catch(FileNotFoundException e)
{
return;
}
}
else
{
String filePath = u.getEncodedPath();
if(filePath == null) return;
Util.loadOTDRFileFromPath(filePath);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6110 次 |
| 最近记录: |