Android GMail/EMail附件读取/存储简析

作者:emneg-zeerd

首发:http://www.itfunz.com/thread-19343-1-1.html

 

一如既往的无图无真相!

我们都知道,Android自带的GMail和EMail都非常蛋疼的不支持附件存储。这就导致很多问题,比如apk无法安装……

下面,简单介绍一下GMail/EMail附件读取存储的准备知识。

既然目标已经明确了,那么首先要做的事情就是在我们点击GMail/EMail的打开附件按钮时,需要相应我们自己编写的应用程序
这里大概用到了一些filter的知识,我的另一篇文章《android文件关联的实现以及参数获取》中曾经简单的提过,有幸去看一看看。
这里还是直接给出xml文件:

[xhtml] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="com.pickattach"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  7.         <activity android:name=".MainActivity"  
  8.                   android:label="@string/app_name">  
  9.             <intent-filter>  
  10.                 <action android:name="android.intent.action.MAIN" />  
  11.                 <category android:name="android.intent.category.LAUNCHER" />  
  12.             </intent-filter>  
  13.             <intent-filter>  
  14.                 <action android:name="android.intent.action.VIEW"></action>  
  15.                 <category android:name="android.intent.category.DEFAULT"></category>  
  16.                 <category android:name="android.intent.category.BROWSABLE"></category>  
  17.                 <data android:scheme="content" android:host="gmail-ls" android:mimeType="*/*"></data>  
  18.             </intent-filter>  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.VIEW"></action>  
  21.                 <category android:name="android.intent.category.DEFAULT"></category>  
  22.                 <category android:name="android.intent.category.BROWSABLE"></category>  
  23.                 <data android:host="com.android.email.attachmentprovider" android:mimeType="*/*" android:scheme="content"></data>  
  24.             </intent-filter>  
  25. </activity>  
  26.     </application>  
  27.     <uses-sdk android:minSdkVersion="7" />  
  28. <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  
  29. <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
  30. </manifest>   

 

里面几个关键点,需要注意。
就是新增的两组“intent-filter”和里面的“data”项。
其中,
“android:host="com.android.email.attachmentprovider" ”对应EMAIL;
“android:host="gmail-ls"”对应GMail。


接下来就是MainActivity了。

针对EMail的MainActivity:

  1. public class MainActivity extends Activity {  
  2.     private static final String FORMAT_RAW = "RAW";  
  3.     private static final String FORMAT_THUMBNAIL = "THUMBNAIL";  
  4.     private static final String E_MAIL_PATH = "com.android.email";  
  5.     /** Called when the activity is first created. */  
  6.     @Override  
  7.     public void onCreate(Bundle savedInstanceState) {  
  8.         super.onCreate(savedInstanceState);  
  9.         setContentView(R.layout.main);  
  10.         Intent intent = getIntent();  
  11.         String action = intent.getAction();  
  12.         if(Intent.ACTION_VIEW.equals(action)){  
  13.             String str = intent.getDataString();  
  14.             if(str != null){  
  15.                 Uri uri = null;  
  16.                 uri = Uri.parse(str);  
  17.                 List<String> segments = uri.getPathSegments();  
  18.                 String dbName = segments.get(0);  
  19.                 String id = segments.get(1);  
  20.                 String format = segments.get(2);  
  21.                   
  22.                 String filename;  
  23.                 if (FORMAT_THUMBNAIL.equals(format)) {  
  24.                     int width = Integer.parseInt(segments.get(3));  
  25.                     int height = Integer.parseInt(segments.get(4));  
  26.                     filename = "thmb_" + dbName + "_" + id;  
  27.                     File dir = getBaseContext().getCacheDir();  
  28.                     File file = new File(dir, filename);  
  29.                     if (!file.exists()) {  
  30.                         // THUMBNAIL格式的处理,这个貌似和本程序无关  
  31.                     }  
  32.                 }  
  33.                 else {  
  34.                     String db = getBaseContext().getDatabasePath(dbName ).toString()+ ".db_att";  
  35.                     filename =  db + File.separator + id;  
  36.                     String str1 = this.getPackageName();  
  37.                     filename = filename.replace(str1, E_MAIL_PATH);  
  38.                       
  39.                     // 拷贝过程,我这里偷懒,直接cat到sdcard的1文件了。  
  40.                     // 实际使用需要写个读写函数,还要从用户交互处获得写入的文件名  
  41.                     runRootCommand("cat " + filename + " > /sdcard/1");  
  42.                 }  
  43.         }      
  44.     }  
  45. }  

 

针对GMail的MainActivity:

  1. public class MainActivity extends Activity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.main);  
  7.         Intent intent = getIntent();  
  8.         String action = intent.getAction();  
  9.         if(Intent.ACTION_VIEW.equals(action)){  
  10.                 try {  
  11.                     InputStream in = getContentResolver().openInputStream(getIntent().getData());  
  12.                     // 输出文件同样,需要增加人机交互  
  13.                     FileOutputStream  fos = new FileOutputStream("/sdcard/a.doc",true);  
  14.                     int ch = 0;  
  15.                     while((ch = in.read()) != -1){  
  16.                         fos.write(ch);  
  17.                     }  
  18.                     fos.close();  
  19.     
  20.                 } catch (MalformedURLException e) {  
  21.                     // TODO Auto-generated catch block  
  22.                     e.printStackTrace();  
  23.                 } catch (IOException e) {  
  24.                     // TODO Auto-generated catch block  
  25.                     e.printStackTrace();  
  26.                 }  
  27.         }      
  28.     }  
  29. }  

 

具体的成品,看我这几天哪天有空做个吧。其实还是很有用的东西。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值