How to share image of imageview?

后端 未结 6 1880
余生分开走
余生分开走 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:11

    You need to add this permission

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
    0 讨论(0)
  • 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

    0 讨论(0)
  • 2021-01-14 17:21

    try creating the cache to store this image first and then share it because you can only share images which are public to other applications also.You cannot share the content which are private to you application.

    use Context.getExternalCacheDir() to create cache and then share the content of this cache

    0 讨论(0)
  • 2021-01-14 17:29
    • get bitmap from imageview
    imageview.buildDrawingCache();
    Bitmap image = mainimg.getDrawingCache();
    
    • convert url
    public Uri getImageUri(Context inContext, Bitmap inImage) {
      ByteArrayOutputStream bytes = new ByteArrayOutputStream();
      inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
      String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
      return Uri.parse(path);
    }
    
    • then share button
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/*");
    share.putExtra(Intent.EXTRA_STREAM, getImageUri(this,getImageUri));
    startActivity(Intent.createChooser(share,"Share via"));
    
    0 讨论(0)
  • 2021-01-14 17:32

    Try below code to share your image:

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("image/*");
        share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg"));
        startActivity(Intent.createChooser(share,"Share via"));
    

    Add these permissions to AndroidMenifest.xml

      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
    0 讨论(0)
  • 2021-01-14 17:35
    Bundle intent = getIntent().getExtras();
    cardView = (CardView) findViewById(R.id.card);
    final String query = intent.getString("Query1");
    
    db = new DataBaseHelper(Image.this);
    
    Cursor c = db.getData(query);
    
    if (c.getCount() != 0) {
        c.moveToFirst();
    
        do {
            image = c.getString(5);
            title=c.getString(3);
        } while (c.moveToNext());
    }
    
    txt.setText(title);
    
    img.setImageDrawable(getResources()
        .getDrawable(getResources().getIdentifier(image, "drawable", getPackageName())));
    
    0 讨论(0)
提交回复
热议问题