Convert Bitmap image into PDF using Glide and share PDF using intent android.

public class GlideUtils {
    private ProgressDialog progressDialog;

    public void downloadImageUsingGlide(String imageUrl, Context mContext, String imgName) {
        String imgUrl = imageUrl;

        int index = imgUrl.lastIndexOf('/') + 1;
//        final String imgName = imgUrl.substring(index);
        final String imageFormat = getImageType(imgUrl);


        Glide.with(mContext)
                .asBitmap()
                .load(imgUrl)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition) {
                        LogUtils.LOGD("dietPlan", "onResourceReady");
                        hideProgress();
                        Intent i = new Intent(Intent.ACTION_SEND);
                       /* i.setType("image/*");
                        i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap, imageFormat, imgName, mContext));*/
                        i.setType("application/pdf");
                        i.putExtra(Intent.EXTRA_STREAM, getPDF3(bitmap, imageFormat, mContext, imgName));
                        mContext.startActivity(Intent.createChooser(i, "Share Image"));

                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                        LogUtils.LOGD("dietPlan", "onLoadCleared");
                    }


                    @Override
                    public void onLoadStarted(@Nullable Drawable placeholder) {
                        super.onLoadStarted(placeholder);
                        LogUtils.LOGD("dietPlan", "onLoadStarted");
                        showProgress(mContext);
                    }

                    @Override
                    public void onLoadFailed(@Nullable Drawable errorDrawable) {
                        super.onLoadFailed(errorDrawable);
                        LogUtils.LOGD("dietPlan", "onLoadFailed");
                        hideProgress();
                    }
                });
    }


    public String getImageType(String imageURL) {
        if (imageURL.endsWith(".png")) {
            return Constants.IMAGE_FORMAT_2;
        } else {
            return Constants.IMAGE_FORMAT_1;
        }
    }

    public static ProgressDialog createProgressDialog(Context mContext) {
        ProgressDialog dialog = new ProgressDialog(mContext);
        try {
            dialog.show();
        } catch (WindowManager.BadTokenException e) {

        }
        dialog.setCancelable(false);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
        dialog.setContentView(R.layout.progress_dialog_layout);
        ProgressBar pb = (ProgressBar) dialog.findViewById(R.id.progressBar1);
        pb.getIndeterminateDrawable().setColorFilter(mContext.getResources().getColor(R.color.colorBullet), PorterDuff.Mode.MULTIPLY);
        dialog.dismiss();
        return dialog;
    }

    public void showProgress(Context mContext) {
        try {
            if (progressDialog == null)
                progressDialog = createProgressDialog(mContext);
            if (!progressDialog.isShowing())
                progressDialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void hideProgress() {
        try {
            if (progressDialog != null && progressDialog.isShowing()) {
//            progressDialog.hide();
                progressDialog.dismiss();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public Uri getPDF3(Bitmap bmp, String imgFormat, Context context, String imageName) {
        Uri pdfUri = null;
        File pdfDir;
        FileOutputStream fos;
        PdfDocument pdfDocument = new PdfDocument();
        try {
            PdfDocument.PageInfo myPageInfo = new PdfDocument.PageInfo.Builder(bmp.getWidth(), bmp.getHeight(), 1).create();
            PdfDocument.Page page = pdfDocument.startPage(myPageInfo);

            page.getCanvas().drawBitmap(bmp, 0, 0, null);
            pdfDocument.finishPage(page);

            pdfDir = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), imageName + ".pdf");
            if(!pdfDir.exists()) {
                LogUtils.LOGD("DietPan","file do not exists");
                fos = new FileOutputStream(pdfDir);
                pdfDocument.writeTo(fos);
                fos.close();
            }else {
                LogUtils.LOGD("DietPan","file exists");
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                pdfUri = FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".provider", pdfDir);
            } else {
                pdfUri = Uri.fromFile(pdfDir);
            }

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

        pdfDocument.close();
        return pdfUri;

    }
}

Comments

Popular Post

Implement search functionality with help of edittext, rxjava and retrofit 2 in android.

QR code scanner using Zxing library. Scanner for android

Store GPS Location in image background android.

Storing captured image or an image picked from a gallery in internal storage using file provider android.

Shortcut for android for every version, android shortcut for oreo, android shortcut for nougat.