3

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

Mark Barr
  • 159
  • 2
  • 12

3 Answers3

1

How about something like:

Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
canvas.drawBitmap(bitmap, ...);
Andrew
  • 36,676
  • 11
  • 141
  • 113
  • I tried Bitmap bitmap = BitmapFactory.decodeFile(fileName); canvas.drawBitmap(bitmap, new Rect(a,120,a+200,270), rectangle, null); but it has a error under he word rectangle what value is rectangle? – Mark Barr Jun 03 '15 at 23:25
  • Try to put there two equal rects new Rect(a,120,a+200,270) – Andrew Jun 03 '15 at 23:31
  • sorry throws an error 06-04 06:13:28.206: E/AndroidRuntime(9993): java.lang.NullPointerException 06-04 06:13:28.206: E/AndroidRuntime(9993): at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1083) – Mark Barr Jun 04 '15 at 05:13
  • This works put the rectangle in the right place and size but it blank and doesnt show bitmap Bitmap bitmap = BitmapFactory.decodeFile("/storage/emulated/0/Pictures/harris1.jpg"); canvas.drawBitmap(bitmap, new Rect(a,120,a+200,270), new Rect(a,120,a+200,270), null); – Mark Barr Jun 04 '15 at 05:51
  • 1
    This does it canvas.drawBitmap(bitmap,null, new Rect(a,120,a+200,270), null); made the source rect null – – Mark Barr Jun 04 '15 at 17:38
0

Use BitmapFactory there's an example you can follow

Reading an image file into bitmap from sdcard, why am I getting a NullPointerException?

Community
  • 1
  • 1
eduyayo
  • 2,020
  • 2
  • 15
  • 35
0

You might like this bit of code:

ImageView myImageView = (ImageView)findViewById(R.id.imageview);
BitmapDrawable imagePath = new BitmapDrawable(getResources(), "/storage/emulated/0/Pictures/harris1.jpg");
myImageView.setImageBitmap(imagePath.getBitmap());
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
  • I dont have an imageview Im trying to print to a canvas which in turn is saved as a PDF using it from resources works fine just cant work out how to use an image from the device – Mark Barr Jun 03 '15 at 23:27