如何分享应用程序的图像

时间:2018-02-16 13:30:03

标签: android

我有可绘画的图片,我想分享那张图片。

以下是我的代码,

 btnic.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.defaultpicture);
            Intent ppp = new Intent("android.intent.action.SEND");
            ppp.setType("image/png");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.PNG, 100, bytes);
            String path = MediaStore.Images.Media.insertImage(getContentResolver(),
                    b, "defaultpicture", null);
            Uri uri =  Uri.parse(path);
            ppp.putExtras(Intent.EXTRA_STREAM, uri); **i am getting error here**
            MainActivity.this.startActivity(Intent.createChooser(ppp, "Send picture"));
            Toast.makeText(getApplicationContext(), "Picture Copied", Toast.LENGTH_SHORT).show();
        }
    });

我在哪里做错了?

2 个答案:

答案 0 :(得分:4)

Intent.EXTRA_STREAM将值Local作为存储上的文件路径而不是资源(非资源ID)。  因此,您需要先将图像保存为存储中的文件,然后将URL传递给共享Intent。 首先解码资源。

Bitmap b = BitmapFactory.decodeResource(res, id);

将其保存到存储上的特定位置,并将此文件路径用作intent中的值。您可以将其保存为以下或以其他方式保存。

String path = MediaStore.Images.Media.insertImage(getContentResolver(),
        b, "myFile", null);

答案 1 :(得分:0)

                final Intent shareIntent = new Intent(Intent.ACTION_SEND);
                shareIntent.setType("image/jpg");
                final File photoFile = new File(getFilesDir(), "yourimage.jpg");
                Log.d("TAG", "onClick: "+photoFile);
                shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
                startActivity(Intent.createChooser(shareIntent, "Share image using"));

了解有关Sharing Content with Intents

的更多信息
相关问题