How can I share images from an Array that uses Ima

2020-05-07 02:47发布

问题:

I have a doubt.

How can I share images from an Array that uses ImageView feature?

My array has more than 100 images, an example:

Final int [] photos = {
                R.drawable.abrir_a_boca,
                R.drawable.rooms,
                R.drawable.firmly,
                R.drawable.agradeca,
                R.drawable.alfaiate,
                R.drawable.ancora,
}

To share I'm trying to use Intent.ACTION_SEND

Set.setOnClickListener (new View.OnClickListener () {
            @Override
            Public void onClick (View v)
            {
                Intent sharingIntent = new Intent (Intent.ACTION_SEND);
                Uri screenshotUri = Uri.parse (photos???);

                SharingIntent.setType ("image / *");
                SharingIntent.putExtra (Intent.EXTRA_STREAM, screenshotUri);
                StartActivity (Intent.createChooser (sharingIntent, "Share image using"));

            }
        });

How can I share the images?

Thank you so much!!!

回答1:

You have to do some operations before you share your image, so add the permission in your Manifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Fist, create a Bitmap object from your drawable resource:

Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.xxxx);

Then, get the path for you share your image

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/yourImage.jpg";
OutputStream out = null;
File file=new File(path);
try {
    out = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
    out.flush();
    out.close();
} catch (Exception e) {
    e.printStackTrace();
}
path=file.getPath();
Uri uri = Uri.parse("file://"+path);

And finally, create your intent:

Intent intent = new Intent();
intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.setType("image/jpg");
startActivity(Intent.createChooser(intent,"Share with..."));