Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert URI to bitmap? [duplicate]

I Am Making An Image Filter App And Before Applying Filter I Will Crop The Image As Square, I Can Crop The Image Using Soundcloud Library Like This:-

private void beginCrop(String sources) {
    img_name = "cropped"+System.currentTimeMillis();
    Uri destination = Uri.fromFile(new File(getCacheDir(), "cropped"+System.currentTimeMillis()));
    Uri source = Uri.parse("file://"+sources);
    Crop.of(source, destination).withAspect(200,200).start(this);
}

private void handleCrop(int resultCode, Intent result) {
    if (resultCode == RESULT_OK) {
        Toast.makeText(this,Crop.getOutput(result).toString(), Toast.LENGTH_SHORT).show();
        Intent intent = new Intent(AlbumActivity.this,FilterImageActivity
                .class);
        intent.putExtra("output_image",Crop.getOutput(result).toString());
        startActivity(intent);
    } else if (resultCode == Crop.RESULT_ERROR) {
        Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
    }
}}

And On The Next Activity Which Is FilterImageActivity.java:-

     @Override
    protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_filter_image);

        String uri = getIntent().getStringExtra("output_image");
        setImageDir(uri);
}

And The Function setImageDir():-

 private void setImageDir(String uri)
    {
        Bitmap bitmap = convetBitmap(uri);

        // clear bitmap memory
       /* originalImage.recycle();
        finalImage.recycle();
        finalImage.recycle();*/

        originalImage = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        filteredImage = originalImage.copy(Bitmap.Config.ARGB_8888, true);
        finalImage = originalImage.copy(Bitmap.Config.ARGB_8888, true);
        imagePreview.setImageBitmap(originalImage);
        bitmap.recycle();

        // render selected image thumbnails
        filtersListFragment.prepareThumbnail(originalImage);
    }

And The Function convertBitmap():-

 private Bitmap convetBitmap(final String uri)
   {
       final Bitmap[] bitmap = {null};
       Thread task = new Thread()
       {
           @Override
           public void run()
           {
               try {
                   bitmap[0] =   Glide.with(getApplicationContext()).asBitmap().load(Uri.parse(uri)).into(100,100).get();
               } catch (InterruptedException | ExecutionException e) {
                   e.printStackTrace();
               }
           }
       };

       task.start();


   return bitmap[0];

}

Image Is Cropped And Saved To Directory For Example

file:///data/user/0/com.dev.pigeon/cache/cropped1518332591373

But It Gives An Error

 Attempt to invoke virtual method 'android.graphics.Bitmap android.graphics.Bitmap.copy(android.graphics.Bitmap$Config, boolean)' on a null object reference

Maybe The URI is Not Converting Into Bitmap Please Any Solution???

like image 877
Pigeon App Avatar asked Feb 11 '18 07:02

Pigeon App


Video Answer


1 Answers

To convert URI to the bitmap you can do as follows.

try {
    if(  Uri.parse(paths)!=null   ){  
     Bitmap bitmap = MediaStore.Images.Media.getBitmap(c.getContentResolver() , Uri.parse(paths));
        }
   }
   catch (Exception e) {
            //handle exception
        }

When you are getting file form file picker or camera intent then you can do this.

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK)
    {
        Uri imageUri = data.getData();
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
    }
}
like image 179
Koushik Mondal Avatar answered Oct 16 '22 02:10

Koushik Mondal