android – 如何在我的应用程序中访问gmail附件数据

前端之家收集整理的这篇文章主要介绍了android – 如何在我的应用程序中访问gmail附件数据前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个使用专门创建的二进制(.gcsb)文件类型的应用程序.这些文件保存在SD卡上的文件夹中.

目前,他们使用ES文件浏览器移动或关闭,或者手机制造商的行为类似于USB驱动器的转移实用程序.笨重.我想要的是能够将文件通过电子邮件发送到手机,然后从gmail中打开文件作为附件,这应该启动应用程序,然后将应用程序保存到相应的SD卡文件夹中.

我发现了一些关于设置意图的东西 – 希望 – 在gmail(特别是Open custom Gmail attachment in my Android app)中单击“预览”启动应用程序,但我完全不确定如何访问文件数据!我想它必须与Intent.get … Extra()函数之一,但是哪个,以及如何使用它?

解决方法

找到了怎么做.希望这可以帮助别人.派对矿,部分来自其他职位.它的目标是处理.gcsb文件附件.

意图过滤器是

  1. <intent-filter>
  2. <action android:name="android.intent.action.VIEW" />
  3. <category android:name="android.intent.category.DEFAULT" />
  4. <category android:name="android.intent.category.BROWSABLE" />
  5. <data android:mimeType="application/octet-stream" />
  6. </intent-filter>

并且活动onCreate()/ onRestart()中的代码

  1. Intent intent = getIntent();
  2. InputStream is = null;
  3. FileOutputStream os = null;
  4. String fullPath = null;
  5.  
  6. try {
  7. String action = intent.getAction();
  8. if (!Intent.ACTION_VIEW.equals(action)) {
  9. return;
  10. }
  11.  
  12. Uri uri = intent.getData();
  13. String scheme = uri.getScheme();
  14. String name = null;
  15.  
  16. if (scheme.equals("file")) {
  17. List<String> pathSegments = uri.getPathSegments();
  18. if (pathSegments.size() > 0) {
  19. name = pathSegments.get(pathSegments.size() - 1);
  20. }
  21. } else if (scheme.equals("content")) {
  22. Cursor cursor = getContentResolver().query(uri,new String[] {
  23. MediaStore.MediaColumns.DISPLAY_NAME
  24. },null,null);
  25. cursor.moveToFirst();
  26. int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
  27. if (nameIndex >= 0) {
  28. name = cursor.getString(nameIndex);
  29. }
  30. } else {
  31. return;
  32. }
  33.  
  34. if (name == null) {
  35. return;
  36. }
  37.  
  38. int n = name.lastIndexOf(".");
  39. String fileName,fileExt;
  40.  
  41. if (n == -1) {
  42. return;
  43. } else {
  44. fileName = name.substring(0,n);
  45. fileExt = name.substring(n);
  46. if (!fileExt.equals(".gcsb")) {
  47. return;
  48. }
  49. }
  50.  
  51. fullPath = ""/* create full path to where the file is to go,including name/ext */;
  52.  
  53. is = getContentResolver().openInputStream(uri);
  54. os = new FileOutputStream(fullPath);
  55.  
  56. byte[] buffer = new byte[4096];
  57. int count;
  58. while ((count = is.read(buffer)) > 0) {
  59. os.write(buffer,count);
  60. }
  61. os.close();
  62. is.close();
  63. } catch (Exception e) {
  64. if (is != null) {
  65. try {
  66. is.close();
  67. } catch (Exception e1) {
  68. }
  69. }
  70. if (os != null) {
  71. try {
  72. os.close();
  73. } catch (Exception e1) {
  74. }
  75. }
  76. if (fullPath != null) {
  77. File f = new File(fullPath);
  78. f.delete();
  79. }
  80. }

它似乎适用于标准的Android gmail和邮件应用程序.根据是否在gmail中按下“下载”(方案文件)或“预览”(方案内容),可以获得两种不同的文件名.

请注意,将活动设置为单个实例非常重要.

猜你在找的Android相关文章