android – 如何在我的应用程序中访问gmail附件数据

我有一个使用专门创建的二进制(.gcsb)文件类型的应用程序.这些文件保存在SD卡上的文件夹中.

目前,他们使用ES文件浏览器移动或关闭,或者手机制造商的行为类似于USB驱动器的转移实用程序.笨重.我想要的是能够将文件通过电子邮件发送到手机,然后从gmail中打开文件作为附件,这应该启动应用程序,然后将应用程序保存到相应的SD卡文件夹中.

我发现了一些关于设置意图的东西 – 希望 – 在gmail(特别是Open custom Gmail attachment in my Android app)中单击“预览”启动应用程序,但我完全不确定如何访问文件数据!我想它必须与Intent.get … Extra()函数之一,但是哪个,以及如何使用它?

解决方法

找到了怎么做.希望这可以帮助别人.派对矿,部分来自其他职位.它的目标是处理.gcsb文件附件.

意图过滤器是

<intent-filter>
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.DEFAULT" />
    <category android:name="android.intent.category.broWSABLE" />
    <data android:mimeType="application/octet-stream" />
</intent-filter>

并且活动onCreate()/ onRestart()中的代码

Intent intent = getIntent();
InputStream is = null;
FileOutputStream os = null;
String fullPath = null;

try {
    String action = intent.getAction();
    if (!Intent.ACTION_VIEW.equals(action)) {
        return;
    }

    Uri uri = intent.getData();
    String scheme = uri.getScheme();
    String name = null;

    if (scheme.equals("file")) {
        List<String> pathSegments = uri.getPathSegments();
        if (pathSegments.size() > 0) {
            name = pathSegments.get(pathSegments.size() - 1);
        }
    } else if (scheme.equals("content")) {
        Cursor cursor = getContentResolver().query(uri,new String[] {
            MediaStore.MediaColumns.disPLAY_NAME
        },null,null);
        cursor.movetoFirst();
        int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.disPLAY_NAME);
        if (nameIndex >= 0) {
            name = cursor.getString(nameIndex);
        }
    } else {
        return;
    }

    if (name == null) {
        return;
    }

    int n = name.lastIndexOf(".");
    String fileName,fileExt;

    if (n == -1) {
        return;
    } else {
        fileName = name.substring(0,n);
        fileExt = name.substring(n);
        if (!fileExt.equals(".gcsb")) {
            return;
        }
    }

    fullPath = ""/* create full path to where the file is to go,including name/ext */;

    is = getContentResolver().openInputStream(uri);
    os = new FileOutputStream(fullPath);

    byte[] buffer = new byte[4096];
    int count;
    while ((count = is.read(buffer)) > 0) {
        os.write(buffer,count);
    }
    os.close();
    is.close();
} catch (Exception e) {
    if (is != null) {
        try {
            is.close();
        } catch (Exception e1) {
        }
    }
    if (os != null) {
        try {
            os.close();
        } catch (Exception e1) {
        }
    }
    if (fullPath != null) {
        File f = new File(fullPath);
        f.delete();
    }
}

它似乎适用于标准的Android gmail和邮件应用程序.根据是否在gmail中按下“下载”(方案文件)或“预览”(方案内容),可以获得两种不同的文件名.

请注意,将活动设置为单个实例非常重要.

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...