Latheesh Reddy
Latheesh Reddy

Reputation: 66

I need to convert a bitmap to uri to send to use it in image cropper library in android?

Here i am making drawable view into bitmap.

     mDrawingPad.setVisibility(View.VISIBLE);
                BitmapDrawable ob = new BitmapDrawable(getResources(), 
              bitmapconv);
                DrawingView mDrawingView=new 
             DrawingView(Previewimage.this);
                mDrawingPad.addView(mDrawingView);
                mDrawingView.setBackground(ob);
                mDrawingView.buildDrawingCache();
                drawbitmap=mDrawingView.getDrawingCache();

I need to convert this into URI to send to image cropper library

       CropImage.activity(uri).start(Previewimage.this);

Upvotes: 0

Views: 480

Answers (3)

Zohaib Hassan
Zohaib Hassan

Reputation: 984

Use this method:

public Uri getImageUri(Context ctx, Bitmap bitmap) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(ctx.getContentResolver(), 
bitmap, "Temp", null);
return Uri.parse(path);
} 

If you are calling this method inside an Activity then call this method like this:

 getImageUri(YourClassName.this, yourbitmap);

But if you are calling this in an Fragment then call like this:

 getImageUri(getActivity(), yourbitmap);

Upvotes: 1

Meikiem
Meikiem

Reputation: 1936

This part of code works:

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    your_bitmap_image.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(your_context.getContentResolver(), your_bitmap_image, "your_title", null);
    Uri uri = Uri.parse(path);

just try to replace the areas that I mentioned in it

in the place of "your_context" if you are in an activity put this:

MediaStore.Images.Media.insertImage(getContentResolver(), your_bitmap_image, "your_title", null);
        Uri uri = Uri.parse(path);

if you are in a fragment :

MediaStore.Images.Media.insertImage(getContext().getContentResolver(), your_bitmap_image, "your_title", null);
        Uri uri = Uri.parse(path);

Upvotes: 1

yash786
yash786

Reputation: 1151

   /*This saveImage method will return String path*/
   path = saveImage();
   Uri uri = Uri.parse(path);        

   /****************************************************/

    private String saveImage()
    {

    Bitmap bitmap;

    mDrawingView.setDrawingCacheEnabled(true);

    mDrawingView.setDrawingCacheQuality(View.DRAWING_CACHE_QUALITY_HIGH);
    mDrawingView.buildDrawingCache();

    bitmap = Bitmap.createBitmap(mDrawingView.getDrawingCache());

    String state = Environment.getExternalStorageState();
    String root = "";
    String fileName = "/MyImage" + System.currentTimeMillis() + "Image"+ ".jpg";
    String parent = "App_Name";
    File mFile;

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        root = Environment.getExternalStorageDirectory().toString();
        mFile = new File(root, parent);
        if (!mFile.isDirectory())
            mFile.mkdirs();

    } else {
        root = FriendsImageSending.this.getFilesDir().toString();
        mFile = new File(root, parent);
        if (!mFile.isDirectory())
            mFile.mkdirs();
    }

    String strCaptured_FileName = root + "/App_Name" + fileName;

    File f = new File(strCaptured_FileName);

    try {

        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 95, bytes);

        FileOutputStream fo;

        fo = new FileOutputStream(f);

        fo.write(bytes.toByteArray());
        fo.close();
        bitmap.recycle();

        System.gc();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return strCaptured_FileName;
}

Upvotes: 2

Related Questions