我需要只使用文件名来显示图像,而不是使用资源id。

ImageView imgView = new ImageView(this);
imgView.setBackgroundResource(R.drawable.img1);

我在可绘制文件夹中有图像img1。我想展示文件中的图像。

我该怎么做呢?


当前回答

mageView.setImageResource(R.id.img);

其他回答

Labeeb是对的,为什么你需要设置图像使用路径,如果你的资源已经放在资源文件夹,

只有当图像存储在SD-Card中时,才需要这种路径。

尝试下面的代码来设置存储在sd卡中的文件中的位图图像。

File imgFile = new  File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

    myImage.setImageBitmap(myBitmap);

}

并将此权限包含在manifest文件中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
String path = Environment.getExternalStorageDirectory()+ "/Images/test.jpg";

File imgFile = new File(path);
if(imgFile.exists())
{
   Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
   ImageView imageView=(ImageView)findViewById(R.id.imageView);
  imageView.setImageBitmap(myBitmap);
}

你可以使用:

ImageView imgView = new ImageView(this);
InputStream is = getClass().getResourceAsStream("/drawable/" + fileName);
imgView.setImageDrawable(Drawable.createFromStream(is, ""));

您可以使用它来访问特定的文件夹并获得特定的图像

 public void Retrieve(String path, String Name)
   {
    File imageFile = new File(path+Name);

    if(imageFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(path+Name);
        myImage = (ImageView) findViewById(R.id.savedImage);
        myImage.setImageBitmap(myBitmap);
        Toast.makeText(SaveImage.this, myBitmap.toString(), Toast.LENGTH_LONG).show();

    }
}

然后你就可以过来了

Retrieve(Environment.getExternalStorageDirectory().toString()+"/Aqeel/Images/","Image2.PNG");
Toast.makeText(SaveImage.this, "Saved", Toast.LENGTH_LONG).show();

所有的答案都过时了。毕加索的作品最好是用于这种目的。它有很多功能,包括背景图像处理。

我有没有提过它非常容易使用:

Picasso.with(context).load(new File(...)).into(imageView);