PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, July 28, 2022

[FIXED] How to convert URI to bitmap?

 July 28, 2022     android, bitmap, crop, java, nullpointerexception     No comments   

Issue

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


Solution

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);
    }
}


Answered By - Koushik Mondal
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing