Ad

How To Convert A View Or Layout To Image And Share Using Intent?

I am trying to convert a card view into image and share it through whats app. I am using file provider as the test device's version is higher.

So when I am trying to create a file it is giving me an exception

e = {[email protected]} "java.lang.IllegalArgumentException: Failed to find configured root that contains /com.example.onboardingversion2/images/card_image"
mCardFile = {[email protected]} "com.example.onboardingversion2/images/card_image"

for the code :

   public void createFile() {

        FileOutputStream outputStream = null;
        try {

      File imagePath = new File(getActivity().getPackageName(), "images");

            mCardFile = new File(imagePath, "card_image");

            Uri contentUri = getUriForFile(getContext(), getActivity().getPackageName() + ".provider", mCardFile);
            sendCard(contentUri);

        } catch (Exception e) {
            e.printStackTrace();
        }
}

When I used below code using getFilesDir() it only goes to whats app intent and no image is shown when I try to share.

     File imagePath = new File(getActivity().getFilesDir(), "images");
            mCardFile = new File(imagePath, "card_image");

            Uri contentUri = getUriForFile(getContext(), getActivity().getPackageName() + ".provider", mCardFile);
            sendCard(contentUri);

Xml file

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path name="my_images" path="images/"/>
</paths>

Manifest

     <provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths" />
    </provider>

Converting view to bitmap

 public Bitmap viewToBitmap(final View view) {

    cardView.post(new Runnable() {
        @Override
        public void run() {
            mBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(mBitmap);
            view.draw(canvas);

            createFile();

        }
    });

    return mBitmap;
}

Share code :

public void sendCard(Uri contentUri)
{

    Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
    whatsappIntent.setType("text/plain");
    whatsappIntent.setPackage("com.whatsapp");
    whatsappIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
    whatsappIntent.putExtra(Intent.EXTRA_STREAM, contentUri);
    whatsappIntent.setType("image/jpeg");
    whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

    try {
        getActivity().startActivity(whatsappIntent);
    } catch (android.content.ActivityNotFoundException ex) {
        Utils.showDialog(getActivity(),"Whats App have not been installed.");
    }
}

I have a card view which I am trying to create to a bitmap and convert a bitmap to image file and share using share intent.

Where am I going wrong? Is the view cant get convert to an image?

Please help. thank you.

Ad

Answer

First add permission

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

use Bitmap from res

Bitmap b =BitmapFactory.decodeResource(getResources(),R.drawable.userimage);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(getContentResolver(),
                b, "Title", null);
Uri imageUri =  Uri.parse(path);
share.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(share, "Select"));
Ad
source: stackoverflow.com
Ad