作者:emneg-zeerd
首发:http://www.itfunz.com/thread-19343-1-1.html
一如既往的无图无真相!
我们都知道,Android自带的GMail和EMail都非常蛋疼的不支持附件存储。这就导致很多问题,比如apk无法安装……
下面,简单介绍一下GMail/EMail附件读取存储的准备知识。
既然目标已经明确了,那么首先要做的事情就是在我们点击GMail/EMail的打开附件按钮时,需要相应我们自己编写的应用程序。
这里大概用到了一些filter的知识,我的另一篇文章《android文件关联的实现以及参数获取》中曾经简单的提过,有幸去看一看看。
这里还是直接给出xml文件:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.pickattach"
- android:versionCode="1"
- android:versionName="1.0">
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".MainActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.VIEW"></action>
- <category android:name="android.intent.category.DEFAULT"></category>
- <category android:name="android.intent.category.BROWSABLE"></category>
- <data android:scheme="content" android:host="gmail-ls" android:mimeType="*/*"></data>
- </intent-filter>
- <intent-filter>
- <action android:name="android.intent.action.VIEW"></action>
- <category android:name="android.intent.category.DEFAULT"></category>
- <category android:name="android.intent.category.BROWSABLE"></category>
- <data android:host="com.android.email.attachmentprovider" android:mimeType="*/*" android:scheme="content"></data>
- </intent-filter>
- </activity>
- </application>
- <uses-sdk android:minSdkVersion="7" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
- <uses-permission android:name="android.permission.INTERNET"></uses-permission>
- </manifest>
里面几个关键点,需要注意。
就是新增的两组“intent-filter”和里面的“data”项。
其中,
“android:host="com.android.email.attachmentprovider" ”对应EMAIL;
“android:host="gmail-ls"”对应GMail。
接下来就是MainActivity了。
针对EMail的MainActivity:
- public class MainActivity extends Activity {
- private static final String FORMAT_RAW = "RAW";
- private static final String FORMAT_THUMBNAIL = "THUMBNAIL";
- private static final String E_MAIL_PATH = "com.android.email";
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Intent intent = getIntent();
- String action = intent.getAction();
- if(Intent.ACTION_VIEW.equals(action)){
- String str = intent.getDataString();
- if(str != null){
- Uri uri = null;
- uri = Uri.parse(str);
- List<String> segments = uri.getPathSegments();
- String dbName = segments.get(0);
- String id = segments.get(1);
- String format = segments.get(2);
- String filename;
- if (FORMAT_THUMBNAIL.equals(format)) {
- int width = Integer.parseInt(segments.get(3));
- int height = Integer.parseInt(segments.get(4));
- filename = "thmb_" + dbName + "_" + id;
- File dir = getBaseContext().getCacheDir();
- File file = new File(dir, filename);
- if (!file.exists()) {
- // THUMBNAIL格式的处理,这个貌似和本程序无关
- }
- }
- else {
- String db = getBaseContext().getDatabasePath(dbName ).toString()+ ".db_att";
- filename = db + File.separator + id;
- String str1 = this.getPackageName();
- filename = filename.replace(str1, E_MAIL_PATH);
- // 拷贝过程,我这里偷懒,直接cat到sdcard的1文件了。
- // 实际使用需要写个读写函数,还要从用户交互处获得写入的文件名
- runRootCommand("cat " + filename + " > /sdcard/1");
- }
- }
- }
- }
针对GMail的MainActivity:
- public class MainActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- Intent intent = getIntent();
- String action = intent.getAction();
- if(Intent.ACTION_VIEW.equals(action)){
- try {
- InputStream in = getContentResolver().openInputStream(getIntent().getData());
- // 输出文件同样,需要增加人机交互
- FileOutputStream fos = new FileOutputStream("/sdcard/a.doc",true);
- int ch = 0;
- while((ch = in.read()) != -1){
- fos.write(ch);
- }
- fos.close();
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
具体的成品,看我这几天哪天有空做个吧。其实还是很有用的东西。