Android get attached filename from gmail app

三世轮回 提交于 2019-12-01 22:11:23

Basically, you need to acquire the file system and get a cursor where the info is stored:

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.Toast;

public class CheckIt extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent theIntent = getIntent();
        String attachmentFileName = "No file name found";
        if (theIntent != null && theIntent.getData() != null) {
            Cursor c = getContentResolver().query(
                theIntent.getData(), null, null, null, null);
            c.moveToFirst();
            final int fileNameColumnId = c.getColumnIndex(
                MediaStore.MediaColumns.DISPLAY_NAME);
            if (fileNameColumnId >= 0)
                attachmentFileName = c.getString(fileNameColumnId);
        }
        Toast.makeText(this, attachmentFileName, Toast.LENGTH_LONG).show();
    }

}

In the cursor returned there is another column - MediaStore.MediaColumns.SIZE. At least that's what I get with GMail on my Desire HD. Just try the above code by operning a mail in GMail and hitting the preview button.

Of course, do not forget to add the following to the activity section in the Manifest.xml (do not drop the android.intent.category.LAUNCHER intent-filter section from the activity otherwise it will not be viewable through the launcher):

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="*/*" /> 
</intent-filter>

What you need to do is register for the different mime types. Something like this:

<intent-filter>
<action name="android.intent.action.VIEW">
<category name="android.intent.category.DEFAULT">
<data mimetype="text/xml">
</intent-filter>

Then Android will list your app as an option when trying to open attachements from Gmail. Then it will start your app with an intent that gives you the uri.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!