我必须从 gmail 应用程序中检索内容的文件名。
我得到的内容 uri 类似于
content://gmail-ls/messages/mymailid%40gmail.com/4/attachments/0.1/BEST/false
我看到一些应用程序从中获取文件名,例如 this app
请就此提供帮助。
提前致谢。
最佳答案
基本上,您需要获取文件系统并获取存储信息的光标:
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();
}
}
在返回的游标中还有另一列 - MediaStore.MediaColumns.SIZE。至少这就是我在 Desire HD 上使用 GMail 得到的结果。只需通过在 GMail 中打开邮件并点击预览按钮来尝试上面的代码。
当然,不要忘记将以下内容添加到 Manifest.xml 中的 Activity 部分(不要从 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>
关于Android从gmail应用程序获取附加文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6031410/