Android 来自外部文件的 Canvas 上的位图

Android Bitmap on Canvas from External File

我使用以下代码从资源文件夹中获取位图,将其放入可绘制对象并将其绘制到 canvas

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

我希望能够从我设备上的图片目录中获取位图并将其绘制到 canvas

我得到的文件路径如下

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

在此实例中,文件名等于“/storage/emulated/0/Pictures/harris1.jpg”

如何更改线路

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

要在设备上使用图片的文件名?

感谢任何帮助

马克

使用 BitmapFactory 有一个您可以遵循的示例

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

怎么样:

Bitmap bitmap = BitmapFactory.decodeFile(pathToFile);
canvas.drawBitmap(bitmap, ...);

您可能会喜欢这段代码:

ImageView myImageView = (ImageView)findViewById(R.id.imageview);
BitmapDrawable imagePath = new BitmapDrawable(getResources(), "/storage/emulated/0/Pictures/harris1.jpg");
myImageView.setImageBitmap(imagePath.getBitmap());