How to share image of imageview?

后端 未结 6 1885
余生分开走
余生分开走 2021-01-14 16:47

I have ImageView and I want to share its image.

Following is my code,

btshare.setOnClickListener(new OnClickListener() {

        @Override
                 


        
6条回答
  •  无人及你
    2021-01-14 17:20

    Final Code so anyone can use to save and share image from imageview :

    View content = findViewById(R.id.full_image_view);
                     content.setDrawingCacheEnabled(true);
    
                         Bitmap bitmap = content.getDrawingCache();
                         File root = Environment.getExternalStorageDirectory();
                         File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
                         try {
                             cachePath.createNewFile();
                             FileOutputStream ostream = new FileOutputStream(cachePath);
                             bitmap.compress(CompressFormat.JPEG, 100, ostream);
                             ostream.close();
                         } catch (Exception e) {
                             e.printStackTrace();
                         }
    
    
                         Intent share = new Intent(Intent.ACTION_SEND);
                         share.setType("image/*");
                         share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
                         startActivity(Intent.createChooser(share,"Share via"));
    
                 }  
    

    happy coding

提交回复
热议问题