How to share image of imageview?

柔情痞子 提交于 2019-12-01 08:48: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" />

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

You need to add this permission

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

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

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