5

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???

0

1 Answer 1

17

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); } } 
Sign up to request clarification or add additional context in comments.

5 Comments

It is Giving This Error:- java.io.FileNotFoundException: No content provider: /storage/emulated/0/DCIM/Retrica/IMG_20180208_195519863.jpg
file path should be like that: file:///test/sdcard/filename.jpg
Thanks, Man You Saved My Day!!!
what is c.getContentResolver()
c seems to be Context

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.