之前从邮箱里面打开文件会调用我们的程序,然后我们的程序将文件通过流的方式读到本地,但是之前在传过来的intent里面没有文件名字的信息,所以一直用“附件”作为文件的名字,这样一是不能正确显示邮件里面文件的真实名字,再就是出现了其他的问题,看到其他的程序却可以显示,刚开始以为是通过底层解析获得的,但发现不是,原来还是通过android的机制就可以获得

还有特写要强调一点的就是,之前在网上搜这个问题的时候都是用的中文搜索的,都没有搜索到解决的办法,今天用英文一搜就搜索出来了,看来以后要习惯用英文搜索。

http://stackoverflow.com/questions/6035535/android-get-email-attachment-name-in-my-application

'm trying to load an email attachment in my application. I can get the content, but I cannot get the file name.

Here's how my intent filter looks like:

    <intent-filter><actionandroid:name="android.intent.action.VIEW" /><categoryandroid:name="android.intent.category.DEFAULT" /><dataandroid:mimeType="image/jpeg" /></intent-filter>

Here is what I get:

INFO/ActivityManager(97): Starting: Intent { act=android.intent.action.VIEW dat=content://gmail-ls/messages/john.doe%40gmail.com/42/attachments/0.1/SIMPLE/false typ=image/jpeg flg=0x3880001 cmp=com.myapp/.ui.email.EmailDocumentActivityJpeg } from pid 97

In my activity I get the Uri and use it to get the input stream for the file content:

InputStream is = context.getContentResolver().openInputStream(uri);

Where can I find the file name in this scenario?

I had the same problem to solve today and ended up finding the solution in another post :Android get attached filename from gmail appThe main idea is that the URI you get can be used both for retrieving the file content and for querying to get more info. I made a quick utility function to retrieve the name :

public static String getContentName(ContentResolver resolver, Uri uri){Cursor cursor = resolver.query(uri, null, null, null, null);cursor.moveToFirst();int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);if (nameIndex >= 0) {return cursor.getString(nameIndex);} else {return null;}
}

You can use it this way in your activity :

Uri uri = getIntent().getData();
String name = getContentName(getContentResolver(), uri);

That worked for me (retrieving the name of PDF files).

Shit 按照上面的步骤来,在我的HTC的手机上是好的,可是跑到联想和魅族的手机上就出问题那个nameIndex返回的是-1,折腾了一下午终于解决了,其实很简单:

public static String getEmailFileName(ContentResolver resolver, Uri uri){Cursor cursor = resolver.query(uri, new String[]{"_display_name"}, null, null, null);cursor.moveToFirst();int nameIndex = cursor.getColumnIndex("_display_name");if (nameIndex >= 0) {return cursor.getString(nameIndex);} else {return null;}}

Android - get email attachment name in my application相关推荐

  1. Android studio运行出现Unable to determine application id: com.android.tools.idea.run.ApkProvisionExcepti

    Android studio运行出现Unable to determine application id: com.android.tools.idea.run.ApkProvisionExcepti ...

  2. 资料仓库——Python Download Exchange E-mail Attachment

    Python Download Exchange E-mail Attachment 参考资料 代码 1 2 考虑时间 3 4 参考资料 https://towardsdatascience.com/ ...

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

    作者:emneg-zeerd 首发:http://www.itfunz.com/thread-19343-1-1.html 一如既往的无图无真相! 我们都知道,Android自带的GMail和EMai ...

  4. android intent email,Android Email Intent

    问题 I've set up two buttons. One opens the compose sms intent and the other opens the compose email i ...

  5. 【Android 安全】DEX 加密 ( Application 替换 | Android 应用启动原理 | ActivityThread 后续分析 | Application 替换位置 )

    文章目录 一.ActivityThread 后续分析 二.ActivityThread 相关源码 三.Application 替换位置 dex 解密时 , 需要将 代理 Application 替换为 ...

  6. dcloud进行android离线打包 需要继承io.dcloud.application.DCloudApplication的问题

    由于项目中使用到了百度地图,且是H5与原生交互操作,既有H5界面 又存在android的原生界面, 在原生界面中如果要使用百度地图需要在Application类中进行初始化,但是由于DCloud已经有 ...

  7. androidの手机Email学习总结

    1. [电子邮件]编辑界面进入设置中点击设置(右上角)抄送/密送地址选项,点击menu键(左下角)仍显示添加抄送/密送地址选项 运行时动态更新菜单项 boolean onPrepareOptionsM ...

  8. andriod驱动之旅-在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务(8)

    我们在Android系统增加硬件服务的目的是为了让应用层的APP能够通过Java接口来访问硬件服务.那么, APP如何通过Java接口来访问Application Frameworks层提供的硬件服务 ...

  9. android edittext email,Android上EditText上的电子邮件地址验证

    慕姐8265434 要执行电子邮件验证,我们有很多方法,但是最简单,最简单的方法是两种方法.1-使用EditText(....).addTextChangedListener它持续在EditText ...

最新文章

  1. vivo手机计算机错误怎么弄,手机计算器出错,原因竟是人性化设计
  2. 学习了哪些知识,计算机视觉才算入门?
  3. Python的线程/进程间通讯对象分析
  4. 网站502与504错误分析
  5. 闲鱼研发框架应用和探索
  6. 央行允许银行倒闭破产,那么储户的存款怎么办?
  7. 用Cucumber JVM编写BDD测试
  8. [Beego] [bootstrap-paginator]实现分页功能
  9. linux远程监控毕业设计,毕业设计论文:基于嵌入式Linux远程监控系统的设计与实现.doc...
  10. 雷林鹏分享:MySQL ALTER命令
  11. 最新dotCMS SQL注入漏洞 攻击者可获得敏感数据 绿盟科技发布安全威胁通告
  12. 美国的时间格式 -- 复杂格式的日期转换 使用Date.parse(“复杂的格式”)方法。
  13. 浮点数 字符串 java_Java如何将浮点数转换为字符串
  14. lopatkin俄大神精简中英俄系统Windows 10 Home 18362.145 19H1 Release x86-x64 RU-EN-CN NANO
  15. 国家多部委发布13份“十四五”规划,115项重大工程​
  16. 通过串口波特率计算数据传输速率(每秒字节数)
  17. linux如何连接蓝牙音箱,蓝牙音箱在Ubuntu 16.04中没有声音
  18. 朗豫:储备池和借贷是MOV重点发力的方向
  19. Javascript:公农历节日周年计算
  20. 高德地图绘制标记点,点击弹出弹框进入第三方地图软件

热门文章

  1. zabbix 系统IO监控_自动发现
  2. Commons Configuration之一简介
  3. C中的extern-static-const关键词
  4. IOS上路_04-简单示例-图片浏览器
  5. Object.keys()返回对象自身可枚举属性组成的数组
  6. python3-知识扩展扫盲易忘-generator的用法
  7. 项目总结一:情感分类项目(emojify)
  8. 一步一步教您用websocket+nodeJS搭建简易聊天室(4)
  9. 使用日程安排自动化测试来更好地管理时间与资源
  10. ElasticSearch入门系列(一)是什么以及安装和运行