Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Bitmap on Canvas from External File

I use the following code to get a bitmap from the resources folder put it into a drawable and paint it to the canvas

 Drawable f = getResources().getDrawable(R.drawable.harris1);
 f.setBounds(a,120,a+200,270);
 f.draw(canvas);

I want to be able to get the bitmap from my pictures directory on my device and paint it on the canvas

I get the filepath as follows

 File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
 String name = "harris1.jpg";
 File file = new File(path, name );
 String fileName = String.format("%s/" + name, path);

in this instance filename is equal to "/storage/emulated/0/Pictures/harris1.jpg"

How do i change the line

Drawable f = getResources().getDrawable(R.drawable.harris);

to use the filname of the picture on the device?

Any Help Appreciated

Mark

like image 985
Mark Barr Avatar asked Sep 15 '26 18:09

Mark Barr


1 Answers

How about something like:

Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
canvas.drawBitmap(bitmap, ...);
like image 184
Andrew Avatar answered Sep 17 '26 08:09

Andrew



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!