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

前端 未结 1 1060
花落未央
花落未央 2021-01-28 09:58

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 [] pho         


        
相关标签:
1条回答
  • 2021-01-28 10:44

    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..."));
    
    0 讨论(0)
提交回复
热议问题