4


我正在尝试通过这样的共享意图共享图像:

Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);

    sharingIntent.setType("image/png");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, application.getString(R.string.app_name));
    sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,application.getString(R.string.app_share_message));

    File image = new File(Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share).toString());
        sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));
        shareMe(sharingIntent);

共享意图正确触发,我选择了 Gmail,一切都按预期运行,直到我按下发送。我收到“无法显示附件”的通知,并且没有它就发送了电子邮件...为什么?

谢谢你的时间。

4

4 回答 4

6

首先,不能保证任何给定的其他应用程序都能够支持android:resource// Uri. 您将在外部存储上共享文件或使用ContentProvider.

话虽如此,替换:

File image = new File(Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share).toString());
    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(image));

和:

    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + C.PROJECT_PATH + "/drawable/" + R.drawable.icon_to_share);

Anandroid:resource://不是 a ,并且您可能会通过转换为 a然后再转换为 aFile来搞砸您的。UriFileUri

于 2013-03-22T12:14:13.990 回答
2

BitmapDrawable bitmapDrawable = (BitmapDrawable)ImageView.getDrawable(); 位图位图 = bitmapDrawable.getBitmap();

        // Save this bitmap to a file.
        File cache = getApplicationContext().getExternalCacheDir();
        File sharefile = new File(cache, "toshare.png");
        Log.d("share file type is", sharefile.getAbsolutePath());
        try {
            FileOutputStream out = new FileOutputStream(sharefile);
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();
        } catch (IOException e) {
            Log.e("ERROR", String.valueOf(e.getMessage()));

        }


        // Now send it out to share
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("image/*");
        share.putExtra(Intent.EXTRA_STREAM,
                Uri.parse("file://" + sharefile));

        startActivity(Intent.createChooser(share,
                "Share Image"));
于 2014-04-05T09:44:38.070 回答
1

就我而言,我使用了:

Uri imageUri = Uri.parse("android.resource://com.examle.tarea/" + R.drawable.tienda_musica);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);           
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);         
startActivity(Intent.createChooser(intent, getString(R.string.action_share))); 
于 2013-11-25T21:19:05.603 回答
0

//打开共享意图选择器(将显示已安装的应用程序,我可以从中共享//图像)

私人无效shareImage(字符串imagePath,字符串quoteByPerson,字符串quoteToShare){

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/*");
        // String imagePath = Environment.getExternalStorageDirectory() +
        // "/myImage.png";
        File imageFileToShare = new File(imagePath);
        Uri uri = Uri.fromFile(imageFileToShare);
        share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        share.putExtra(Intent.EXTRA_TEXT, "Quote of the day-:" + "\n" + quoteToShare + "\n" + quoteByPerson);

        share.putExtra(android.content.Intent.EXTRA_TITLE, "Quote of the day-:");

        if (imagePath.contains("android.resource://"))
        {
            Uri imageUri = Uri.parse(imagePath);
            share.putExtra(Intent.EXTRA_STREAM, imageUri);

        } else
        {
            share.putExtra(Intent.EXTRA_STREAM, uri);
        }
        startActivity(Intent.createChooser(share, "Share inspiration via..."));
    }
于 2015-07-15T21:19:03.757 回答