Khanza dhie
Khanza dhie

Reputation: 31

How to use bitmap to send to another app

can someone help me how to send an image to another application in android?

I want to send an image to be used as wallpaper, BBM display picture, and other applications that can use it (like WhatsApp, contacts, etc.)

I use this code, but it can only be used for sending text and not images as I want

Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);

Then I tried to use this code to set the wallpaper

public void setAsWallpaper(Bitmap bitmap) {
        try {           
            WallpaperManager wm = WallpaperManager.getInstance(_context);
            wm.setBitmap(bitmap);

            //disinimas
            Toast.makeText(_context,
                    _context.getString(R.string.toast_wallpaper_set),
                    Toast.LENGTH_SHORT).show();
        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(_context,
                    _context.getString(R.string.toast_wallpaper_set_failed),
                    Toast.LENGTH_SHORT).show();
        }
    }

the problem with the code, bitmap images directly applied as wallpaper. I wanted like sending text above, the user can choose to use another application. So I want a bitmap image that can later be used for wallpaper, BBM display picture, or other applications that support it

bitmap variable already contains the image that I want to, the images obtained from the internet with this code:

Bitmap bitmap = ((BitmapDrawable) fullImageView.getDrawable())
                .getBitmap();

I use this code and its work, but give me a message BBM: File not found, WhatsApp: File is not an image:

Bitmap icon = bitmap;
    Intent share = new Intent(Intent.ACTION_ATTACH_DATA);
    share.setType("image/jpeg");

    ContentValues values = new ContentValues();
    values.put(Images.Media.TITLE, "title");
    values.put(Images.Media.MIME_TYPE, "image/jpeg");
    Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
            values);


    OutputStream outstream;
    try {
        outstream = getContentResolver().openOutputStream(uri);
        icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
        outstream.close();
    } catch (Exception e) {
        System.err.println(e.toString());
    }

    share.putExtra(Intent.EXTRA_STREAM, uri);
    startActivity(Intent.createChooser(share, "Share Image"));

Upvotes: 1

Views: 1337

Answers (2)

Khanza dhie
Khanza dhie

Reputation: 31

Thanks for your help Antrromet, finally I solved my problem with the following code:

Bitmap icon = bitmap;
        //  Intent share = new Intent(Intent.ACTION_SEND);
        //  share.setType("image/*");

            ContentValues values = new ContentValues();
            values.put(Images.Media.TITLE, "title");
            values.put(Images.Media.MIME_TYPE, "image/*");
            Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI,
                    values);


            OutputStream outstream;
            try {
                outstream = getContentResolver().openOutputStream(uri);
                icon.compress(Bitmap.CompressFormat.JPEG, 100, outstream);
                outstream.close();
            } catch (Exception e) {
                System.err.println(e.toString());
            }

            Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            intent.setDataAndType(uri, "image/*");
            intent.putExtra("mimeType", "image/*");
            this.startActivity(Intent.createChooser(intent, "Set as:"));

Now the image can be used as Wallpaper, BBM profile picture, WA display Picture, Coontact display picture, etc. Thanks

Upvotes: 2

Antrromet
Antrromet

Reputation: 15424

Did you try using the following code? It is use to send binary data to other apps.

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
shareIntent.setType("image/jpeg");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));

Or if you dont have the URI with you but have the Bitmap instead, then try using the code as given here.

UPDATE:

Setting wallpaper and profile picture for BBM are totally different things and there is no common intent for them. For setting the wallpaper, you can try the following as given here.

Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
    intent.addCategory(Intent.CATEGORY_DEFAULT);
    intent.setDataAndType(uri, "image/jpeg");
    intent.putExtra("mimeType", "image/jpeg");
    this.startActivity(Intent.createChooser(intent, "Set as:"));

For BBM, the APIs are not open for changing the user image. So you cannot do that through your app.

Upvotes: 0

Related Questions