0

I am reading a vector image from a XML resource and showing it in an ImageView. It's Ok. Now, I need to pass the image to an Intent using intent.putExtra. Question: How can I convert the vector XML to a bitmap or get the image from the ImageView? I tried .getDrawingCache(), .getDrawable, getImageMatrix, etc, but it does not work.

Tried this way:

String path = MediaStore.Images.Media.insertImage(getApplicationContext().getContentResolver(),
            imgPrevisao.getDrawingCache(),
            "Minha previsão",null);

    Intent sharingIntent = new Intent(Intent.ACTION_SEND);
    sharingIntent.setType("image/*");
    sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path)) ;
    getApplicationContext().startActivity(Intent.createChooser(sharingIntent, "Share with"));

And this way:

    Intent intent = new Intent( Intent.ACTION_SEND );

    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.putExtra( Intent.EXTRA_TEXT, textoPrevisao );
    intent.putExtra( Intent.EXTRA_STREAM, imgPrevisao.getDrawingCache() );
    intent.setType( "image/*" );
    startActivity( intent );

TIA,

André Corrêa

2 Answers2

0

One of the ways is to use MediaStore:

public static Uri getImageUri(Context context, Bitmap bmp) {
    String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), , "share_title",bmp ,null);
    return Uri.parse(path);
}

and than:

public static void shareImageViewContent(ImageView view) { 
   Bitmap bitmap = ((BitmapDrawable)view.getDrawable()).getBitmap();
   Intent sharingIntent = new Intent(Intent.ACTION_SEND);
   sharingIntent.setType("image/jpeg");
   sharingIntent.putExtra(Intent.EXTRA_STREAM, getImageUri(bitmap)) ;
   context.startActivity(Intent.createChooser(sharingIntent, "Share with"));
}

A bit more complicated solution is to use FileProvider Credits: link, link, link

Graham
  • 7,431
  • 18
  • 59
  • 84
j2ko
  • 2,479
  • 1
  • 16
  • 29
  • Thank you j2ko, I'll try that. I don't have reputation enough to upvote your answer, sorry. – André Aranha Feb 13 '17 at 23:03
  • @AndréAranha np, I'm glad to help. But I guess you could mark my solution as answer – j2ko Feb 13 '17 at 23:04
  • it does not work. I'm still trying. I'll post what I've tried. – André Aranha Feb 13 '17 at 23:29
  • Are you sure that `getDrawingCache()` returns not `null`?Did you call `imgPrevisao.buildDrawingCache()` to be sure that it's not null? Your second solution is incorrect - `intent.putExtra( Intent.EXTRA_STREAM, imgPrevisao.getDrawingCache() );` - `EXTRA_STREAM` should contain `Uri` not a bitmap object. – j2ko Feb 15 '17 at 11:38
  • yes, I tried using imgPrevisao.buildDrawingCache() before calling getDrawingCache(). I'm busy lately, will try to debug again and post a better description of the error. Thank you for the reply. – André Aranha Feb 16 '17 at 11:52
0

I know it was a long time ago, but if someone ends up having this same doubt, I hope it helps. I wanted to share an image and a text, as I couldn't do it the way I'd like, I made a screenshot and shared it.

private void shareScreenShot() {

try {
    // image naming and path  to include sd card  appending name you choose for file
    String mPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
                   .toString()+"/Zodiaco";

    // create bitmap screen capture
    View v1 = getWindow().getDecorView().getRootView();
    v1.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    //creates the directory
    File pasta = new File(mPath);
    if (!pasta.exists()) {
        pasta.mkdir();
    }

    //creates the image
    File imageFile = new File(mPath+ "/" + getDate() + ".jpg");
    FileOutputStream outputStream = new FileOutputStream(imageFile);
    if(!imageFile.exists()){
        imageFile.createNewFile();
    }
    int quality = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
    outputStream.flush();
    outputStream.close();

    Uri uri = Uri.fromFile(imageFile);

    // creates the intent and defines its action
    Intent intent = new Intent( Intent.ACTION_SEND );
    intent.setDataAndType(uri, "image/*");
    intent.putExtra(Intent.EXTRA_STREAM, uri);
    // inicia a intent
    startActivity( intent );

} catch (Throwable e) {

    Toast.makeText(this, "Não foi possível compartilhar a previsão.",
                   Toast.LENGTH_LONG).show();

    e.printStackTrace();
}

}