0

I am using an Intent to share an image, like elsewhere is explained on stackoverflow. Here is the code I stole from this site (thanks) :)

private void share(String nameApp, String imagePath) {
    List<Intent> targetedShareIntents = new ArrayList<Intent>();
    Intent share = new Intent(android.content.Intent.ACTION_SEND);
    share.setType("image/jpeg");
    List<ResolveInfo> resInfo = getPackageManager().queryIntentActivities(share, 0);
    if (!resInfo.isEmpty()){
        for (ResolveInfo info : resInfo) {
            Intent targetedShare = new Intent(android.content.Intent.ACTION_SEND);
            targetedShare.setType("image/jpeg"); // put here your mime type

            if (info.activityInfo.packageName.toLowerCase().contains(nameApp) || 
                    info.activityInfo.name.toLowerCase().contains(nameApp)) {
                targetedShare.putExtra(Intent.EXTRA_TEXT,     "My body of post/email");
                targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) );
                targetedShare.setPackage(info.activityInfo.packageName);
                targetedShareIntents.add(targetedShare);
            }
        }

        Intent chooserIntent = Intent.createChooser(targetedShareIntents.remove(0), getResources().getString(R.string.share_select));
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));
        startActivity(chooserIntent);
    }
}

My problem is that I don't have any jpg to share, but I create the images, with drawables and layer-lists like this:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/background01" />
    <item android:drawable="@drawable/img02" />
    <item android:drawable="@drawable/img07" />
</layer-list>

I don't understand if and how I can use these drawables as they were full images and use them inside the share() function. Can some one please explain me?

Thank you in advance.

Beppi's
  • 2,089
  • 1
  • 21
  • 38
  • Simply by communicating their `R.drawable` id? – class stacker Apr 05 '13 at 10:03
  • But that's not a jpg, and the function needs a path, how can I generate it from my drawable id? – Beppi's Apr 05 '13 at 10:05
  • Oh sorry, my fault. You want to send an email? Then you'll have to render the XML drawable to a Canvas of a given size, store that and pass it on. – class stacker Apr 05 '13 at 10:10
  • Thank you very much. Now it's clearer. Given the fact that I must render the image to a phisical file to manage it, is there a way to render it directly from the xml, as you know? – Beppi's Apr 05 '13 at 10:20

1 Answers1

1

thats how you get a bitmap by two overlaying bitmap images:

 private Bitmap overlay(Bitmap bmp1, Bitmap bmp2) {
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());
    Canvas canvas = new Canvas(bmOverlay);
    canvas.drawBitmap(bmp1, new Matrix(), null);
    canvas.drawBitmap(bmp2, new Matrix(), null);
    return bmOverlay;
}

once you get the bitmap you can save it anywhere and share it through your method.

Saad Asad
  • 2,528
  • 3
  • 20
  • 27
  • Thank you. This could solve my problem but generates a lot of work, as soon as I need to write code for any single different xml I have. As you know, is there a way to render the bitmap starting from a drawable.xml, instead directly from other bitmaps? Something like a overlay(int resId) function? – Beppi's Apr 05 '13 at 10:18
  • 1
    sure you can get bitmap directly from resource id like Bitmap mp=BitmapFactory.decodeResource(getResources(), R.drawable.image); so your method calling will be like overlay(mp,mp1); – Saad Asad Apr 05 '13 at 10:27
  • I will mix your solution with this: http://stackoverflow.com/questions/3035692/how-to-convert-a-drawable-to-a-bitmap I really don't need anyway to make an overlay, rather to integrate the bitmap with the sharing function I included. – Beppi's Apr 05 '13 at 10:33
  • 1
    @BeppiMenozzi You may want to use [BitmapFactory.decodeResource(android.content.res.Resources, int, android.graphics.BitmapFactory.Options)](http://developer.android.com/reference/android/graphics/BitmapFactory.html#decodeResource(android.content.res.Resources, int, android.graphics.BitmapFactory.Options)) to be able to specify the resulting size. – class stacker Apr 05 '13 at 10:34
  • Once I have a bitmap, is it necessary to save it to a file and reload it inside targetedShare.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(imagePath)) ); ? – Beppi's Apr 05 '13 at 10:41
  • yes, make an image on sdcard, use the same image always and override this when ever sharing new image – Saad Asad Apr 05 '13 at 10:53