如何访问Gmail附件数据在我的应用程序 [英] How to access gmail attachment data in my app

查看:329
本文介绍了如何访问Gmail附件数据在我的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an app that uses a specifically created binary (.gcsb) file type. These files are kept in a folder on the sdcard.

在它们移动和关闭使用ES文件浏览器或手机制造商的时刻表现得像一个USB驱动器传输工具。沉闷。我想什么来是能够将文件通过电子邮件发送到手机上,然后在Gmail中打开该文件作为附件,这应该火起来的应用程序,然后将其保存到相应的SD卡文件夹。

At the moment they are moved on and off using ES file explorer or the phone manufacturer 'behave like a USB drive' transfer utilities. Clunky. What I want to to is be able to email the files to the phone, then to open the files as attachments from within gmail, which should fire up the app, which will then save them to the appropriate SD card folder.

我发现了一些东西有关意图设置为 - 希望 - 开始点击'preVIEW应用程序在Gmail内(特别是<一个href="http://stackoverflow.com/questions/13916560/open-custom-gmail-attachment-in-my-android-app">Open自定义Gmail附件在我的Andr​​oid应用程序),但什么我不知道的都是如何访问文件数据!我想这一定是随着Intent.get ...额外的()函数之一,但是,以及如何使用它?

I've found some stuff about setting up intents to - hopefully - start the app in clicking 'preview' within gmail (specifically Open custom Gmail attachment in my Android app), but what I'm not at all sure of is how to access the file data! I guess it must be with one of the Intent.get...Extra() functions, but which, and how to use it?

推荐答案

发现了如何做到这一点。希望这可以帮助别人。党的矿山,一部分来自其他职位。它的目标是处理.gcsb文件附件。

Found out how to do it. Hopefully this may help someone else. Party mine, partly from other posts. It is aiming to handle .gcsb file attachments.

意向过滤器

<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>

和code。在活动的onCreate()/ onRestart()是

and the code in the activity onCreate() / onRestart() is

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, 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, 0, 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和邮件应用程序的工作。文件名是根据获得两种不同的方法,如果下载(方案文件)或'preVIEW'(计划内容)是pressed在Gmail中。

It appears to work in the standard Android gmail and mail applications. The file name is obtained two different ways depending if 'download' (scheme file) or 'preview' (scheme content) was pressed in gmail.

请注意,这是极为重要的是活性不设定为单个实例

Note that it is extremely important that the activity is not set to be single instance.

这篇关于如何访问Gmail附件数据在我的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆