Android:在 htc Hero 上选择 Gmail 应用程序时,带有 EXTRA_STREAM 的 Intent.ACTION_SEND 不会附加任何图像

发布于 2024-08-20 12:50:26 字数 1902 浏览 9 评论 0原文

在具有默认邮件应用程序的模拟器上一切正常。但是,当我收到我的 Hero 使用 Gmail 应用程序发送的邮件时,我没有附件。英雄上的默认邮件应用程序运行良好。

如何使此代码与 Hero 上的 Gmail 应用程序配合使用?
您可以看到下面的代码。

    private void startSendIntent() {
        Bitmap bitmap = Bitmap.createBitmap(editableImageView.getWidth(), editableImageView.getHeight(), Bitmap.Config.RGB_565);
        editableImageView.draw(new Canvas(bitmap));
        File png = getFileStreamPath(getString(R.string.file_name));
        FileOutputStream out = null;
        try {
            out = openFileOutput(getString(R.string.file_name), MODE_WORLD_READABLE);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) out.close();
            }
            catch (IOException ignore) {}
        }
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(png));
        emailIntent.setType("image/png");
        startActivity(Intent.createChooser(emailIntent, getString(R.string.send_intent_name)));
}

在日志中我看到以下内容:

02-05 17:03:37.526: DEBUG/Gmail(11511): URI FOUND:file:///sdcard/DCIM/100MEDIA/IMAG0001.jpg
02-05 17:03:37.535: DEBUG/Gmail(11511): ComposeActivity added to message:0 attachment:|IMAG0001.jpg|image/jpeg|0|image/jpeg|LOCAL_FILE|file:///sdcard/DCIM/100MEDIA/IMAG0001.jpg size:0
02-05 17:03:37.585: INFO/Gmail(11511): >>>>> Attachment uri: file:///sdcard/DCIM/100MEDIA/IMAG0001.jpg
02-05 17:03:37.585: INFO/Gmail(11511): >>>>>           type: image/jpeg
02-05 17:03:37.585: INFO/Gmail(11511): >>>>>           name: IMAG0001.jpg
02-05 17:03:37.585: INFO/Gmail(11511): >>>>>           size: 0

谢谢您的回答。

On the Emulator with a default mail-app all works fine. But I have no attach when I'am receiving a mail which I've sent from my Hero using a Gmail app. The default Mail app on the hero works fine.

How can I make this code works with Gmail app on Hero?
You can see the code below.

    private void startSendIntent() {
        Bitmap bitmap = Bitmap.createBitmap(editableImageView.getWidth(), editableImageView.getHeight(), Bitmap.Config.RGB_565);
        editableImageView.draw(new Canvas(bitmap));
        File png = getFileStreamPath(getString(R.string.file_name));
        FileOutputStream out = null;
        try {
            out = openFileOutput(getString(R.string.file_name), MODE_WORLD_READABLE);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) out.close();
            }
            catch (IOException ignore) {}
        }
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(png));
        emailIntent.setType("image/png");
        startActivity(Intent.createChooser(emailIntent, getString(R.string.send_intent_name)));
}

in Logs I see the following:

02-05 17:03:37.526: DEBUG/Gmail(11511): URI FOUND:file:///sdcard/DCIM/100MEDIA/IMAG0001.jpg
02-05 17:03:37.535: DEBUG/Gmail(11511): ComposeActivity added to message:0 attachment:|IMAG0001.jpg|image/jpeg|0|image/jpeg|LOCAL_FILE|file:///sdcard/DCIM/100MEDIA/IMAG0001.jpg size:0
02-05 17:03:37.585: INFO/Gmail(11511): >>>>> Attachment uri: file:///sdcard/DCIM/100MEDIA/IMAG0001.jpg
02-05 17:03:37.585: INFO/Gmail(11511): >>>>>           type: image/jpeg
02-05 17:03:37.585: INFO/Gmail(11511): >>>>>           name: IMAG0001.jpg
02-05 17:03:37.585: INFO/Gmail(11511): >>>>>           size: 0

Thank you for the answer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

二货你真萌 2024-08-27 12:50:26

对我来说,问题是通过以下代码行解决的:

Bitmap screenshot = Bitmap.createBitmap(_rootView.getWidth(), _rootView.getHeight(), Bitmap.Config.RGB_565);
_rootView.draw(new Canvas(screenshot));

String path = Images.Media.insertImage(getContentResolver(), screenshot, "title", null);
Uri screenshotUri = Uri.parse(path);

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
emailIntent.setType("image/png");

startActivity(Intent.createChooser(emailIntent, "Send email using"));

关键是我将屏幕截图保存到媒体库,然后它可以从那里成功发送文件。

For me the problem was solved with the following lines of code:

Bitmap screenshot = Bitmap.createBitmap(_rootView.getWidth(), _rootView.getHeight(), Bitmap.Config.RGB_565);
_rootView.draw(new Canvas(screenshot));

String path = Images.Media.insertImage(getContentResolver(), screenshot, "title", null);
Uri screenshotUri = Uri.parse(path);

final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
emailIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
emailIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
emailIntent.setType("image/png");

startActivity(Intent.createChooser(emailIntent, "Send email using"));

The key thing is that I'm saving the screen-shot to the media library and then it can successfully send a file from there.

子栖 2024-08-27 12:50:26

getFileStreamPath()openFileOutput() 将在其他应用程序(即 Gmail)无法访问的私有目录中创建文件。使用外部存储创建可公开访问的文件:

private static final int REQUEST_SHARE = 39714;

private File png = null;

private void startSendIntent() {
    png = new File(new File(Environment.getExternalStorageDirectory(), "Pictures"), getString(R.string.file_name));

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(png);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) out.close();
        }
         catch (IOException ignore) {}
        }
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(png));
        emailIntent.setType("image/png");
        startActivityForResult(Intent.createChooser(emailIntent, getString(R.string.send_intent_name)), REQUEST_SHARE);
    }
}

当外部存储不可用时(例如将其安装为 USB 驱动器时),此操作将不起作用。请参阅存储 API 指南,了解有关检测外部是否存在的更多信息存储空间可用。

如果您的最低 API 级别为 8 或更高,则可以使用 Context.getExternalCacheDir()Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) 作为父目录。

除非您最终使用 getExternalCacheDir(),否则请确保使用唯一的文件名以防止用户的文件之一被意外覆盖。

最后,您可以重写 onActivityResult() 以在共享操作完成后删除文件:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == REQUEST_SHARE) {
        if(this.png == null) {
            return;
        }

        this.png.delete();
    }
}

getFileStreamPath() or openFileOutput() will create files in a private directory that is inaccessible to other apps (i.e. Gmail). Use external storage to create publicly-accessible files:

private static final int REQUEST_SHARE = 39714;

private File png = null;

private void startSendIntent() {
    png = new File(new File(Environment.getExternalStorageDirectory(), "Pictures"), getString(R.string.file_name));

    FileOutputStream out = null;
    try {
        out = new FileOutputStream(png);
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) out.close();
        }
         catch (IOException ignore) {}
        }
        final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
        emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(png));
        emailIntent.setType("image/png");
        startActivityForResult(Intent.createChooser(emailIntent, getString(R.string.send_intent_name)), REQUEST_SHARE);
    }
}

This will not work when the external storage is unavailable, like when it is mounted as a USB drive. See the Storage API Guide for more about detecting whether external storage is available.

If your minimum API level is 8 or above, you can use Context.getExternalCacheDir() or Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) as the parent directory.

Unless you end up using getExternalCacheDir(), make sure you use a unique filename to prevent one of the users' files from accidentally getting overwritten.

Finally, you can override onActivityResult() to delete the file after the share operation completes:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if(requestCode == REQUEST_SHARE) {
        if(this.png == null) {
            return;
        }

        this.png.delete();
    }
}
云巢 2024-08-27 12:50:26

我也有同样的症状。就我而言,这是因为我最初使用 MODE_PRIVATE 权限保存附件。当我将其更改为 MODE_WORLD_READABLE 后,Gmail 似乎就能够访问该文件并正确发送附件。虽然我在您的代码片段中看到您使用的是 MODE_WORLD_READABLE ...很奇怪。

http://developer.android.com/reference/android/content/Context .html#MODE_WORLD_READABLE

I had the same symptoms. In my case it was because I was initially saving the attachment with the permissions MODE_PRIVATE. As soon as I changed it to MODE_WORLD_READABLE it seems Gmail was then able to access the file and send the attachment properly. Although I see in your code snippet you were using MODE_WORLD_READABLE ... weird.

http://developer.android.com/reference/android/content/Context.html#MODE_WORLD_READABLE

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文