从外部存储显示?

Displaying from external storage?

我的图片文件夹中保存了一张图片,如何在图像视图中显示它?

喜欢:

imageview.setimage("//Pictures//cat.jpg)

我知道这不是一个正确的代码,但我想实现这样的东西,希望有人能帮助,谢谢!

您首先从文件路径生成一个位图,然后将该位图放入 imageview

File image = new File(filePath);
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);

编辑:同时在您的清单中添加此权限

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

您可以从 sd 卡中像这样设置图像获取路径创建文件变量解码文件 使用 BitmapFactory 设置 imageview 图像

String path = Environment.getExternalStorageState()+"/Pictures//cat.jpg";
File f = new File(path);
imageview.setImageBitmap(new BitmapFactory.decodeFile(f.getAbsolutePath()));

首先你传递了错误的文件路径。

为了正确的文件路径,请这样做

Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath());

然后创建你的 URL 赞。

String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath()) + "/cat.jpg";

那就这样使用吧

File image = new File(filePath);
imageView.setImageBitmap(new BitmapFactory.decodeFile(image.getAbsolutePath()));

结合使用 2 个发布的代码,它可以正常工作,谢谢大家,这是工作代码:

Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES).getAbsolutePath();
    String filePath = Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_PICTURES) + "/picFolder/1.jpg";
    File image = new File(filePath);
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    Bitmap bitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
    image1.setImageBitmap(bitmap);