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

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

问题描述

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

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.

我发现了一些关于设置意图的东西 - 希望 - 通过单击 gmail 中的预览"来启动应用程序(特别是 在我的 Android 应用中打开自定义 Gmail 附件),但我完全不确定如何访问文件数据!我想它一定是 Intent.get...Extra() 函数之一,但是哪个以及如何使用它?

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>

而活动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 和邮件应用程序中运行.文件名的获取方式有两种,具体取决于在 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天全站免登陆