Android从gmail应用程序获取附加文件名

Jan*_*ana 3 gmail android

我必须从gmail应用程序中检索内容的文件名.

我得到的内容与某些东西类似

内容://gmail-ls/messages/mymailid%40gmail.com/4/attachments/0.1/BEST/false

我看到一些应用程序从这个应用程序获取文件名

请帮忙.

提前致谢.

Kam*_*men 11

基本上,您需要获取文件系统并获取存储信息的光标:

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();
    }

}
Run Code Online (Sandbox Code Playgroud)

在返回的游标中有另一列 - MediaStore.MediaColumns.SIZE.至少这是我在Desire HD上使用GMail获得的.只需通过操作GMail中的邮件并点击预览按钮来尝试上述代码.

当然,不要忘记将以下内容添加到Manifest.xml中的activity部分(不要从活动中删除android.intent.category.LAUNCHER intent-filter部分,否则将无法通过启动器查看):

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="*/*" /> 
</intent-filter>
Run Code Online (Sandbox Code Playgroud)

  • 这段代码有两个问题:使用后你需要[`close()`](http://developer.android.com/reference/android/database/Cursor.html#close%28%29)光标,你应该只通过将`new String [] {MediaStore.MediaColumns.DISPLAY_NAME}`作为第二个参数(`projection`)传递给`ContentResolver.query()`来请求你需要的列. (6认同)
  • 点击此处:http://carvingcode.blogspot.com/2009/08/how-to-open-gmail-attachments-with.html (2认同)