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

心已入冬 提交于 2019-12-20 07:32:17

问题


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..."));


来源:https://stackoverflow.com/questions/43872444/how-can-i-share-images-from-an-array-that-uses-imageview-feature

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!