我必须通过我的应用程序从gmail应用程序打开附件文件.
我在内容模式中获得链接://gmail-ls/messages/mailid%40gmail.com/4/attachments/0.1/BEST/false
我的问题是链接对于邮件客户端中的每个文件都不是唯一的.一个或多个文件具有相同的Uri.
有没有办法获取文件名或电子邮件发送日期,以便我可以解决这个问题.
提前致谢.
如下所示,您可以制作相同附件的本地副本并处理该文件.直接你无法访问gmail服务器上的那个文件.
Uri uri = getIntent().getData();
if (uri != null) {
try {
InputStream attachment = getContentResolver().openInputStream(uri);
if (attachment == null)
Log.e("GMAIL ATTACHMENT", "Mail attachment failed to resolve");
else {
FileOutputStream tmp = new FileOutputStream(getCacheDir().getPath() + "/temp.myfile");
byte[] buffer = new byte[1024];
while (attachment.read(buffer) > 0)
tmp.write(buffer);
tmp.close();
attachment.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return getCacheDir().getPath() + "/temp.myfile";
Run Code Online (Sandbox Code Playgroud)
Reember将此添加到您的清单中.
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<action android:name="android.intent.action.EDIT" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="*"
android:mimeType="application/octet-stream"
android:pathPattern=".*\\.tsconfig" />
<!-- To handle gmail attachments -->
</intent-filter>
Run Code Online (Sandbox Code Playgroud)
我通过间接的方式解决了这个问题。
我使用文件大小来识别唯一性。
如果您需要这里的代码,那就是
InputStream is = getContentResolver().openInputStream(uri);
FILE_SIZE=is.available();
Run Code Online (Sandbox Code Playgroud)