如何在 ImageButton 上动态设置图像?

How to set Image on ImageButton dynamically?

我想通过 SD 卡上的文件动态地在我的应用程序的按钮上设置图像。我试过这段代码,但它不工作。我试图将图像转换为位图对象并将该对象设置为 ImageButton,但它没有显示任何内容。我该如何解决这个问题?

我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File  imageFile = new File("/sdcard0/DCIM/camera/jbn.jpg");
    Bitmap bmp = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

    ImageButton button1 = (ImageButton)findViewById(R.id.imgBtn);
    button1.setImageBitmap(bmp);
}

   XML 

   <ImageButton
   android:layout_width="200dip"
   android:layout_height="200dip"
   android:id="@+id/imgBtn"
   />

算法

void loadPic()
  {
      String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
      String pathName = baseDir + "/DCIM/camera/";
      File parentDir=new File(pathName);

      File[] files = parentDir.listFiles();
      Date lastDate;
      String lastFileName;
      boolean isFirstFile = true; //just temp variable for being sure that we are on the first file
      for (File file : files) {
          if(isFirstFile){
              lastDate = new Date(file.lastModified());
              isFirstFile = false;
          }
          if(file.getName().endsWith(".jpg") || file.getName().endsWith(".jpeg")){
              Date lastModDate = new Date(file.lastModified());
              if (lastModDate.after(lastDate))  {
                  lastDate = lastModDate;
                  lastFileName = file.getName();
              }
          }
      }

尝试获取图像文件的绝对路径:

File  imageFile = new File("/sdcard0/DCIM/camera/jibin.jpg");
Bitmap bmp = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

您可以尝试这样的操作 -

    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);
    }

如果您想从任何 URL 动态设置图像,请按这种方式设置图像。您还可以设置位图的宽度和高度。

private class LoadImage extends AsyncTask<Bundle, String, Bitmap> {
    Bitmap bitmap;
    @Override
    protected Bitmap doInBackground(Bundle... args) {
        extras=args[0];
        try {
            InputStream in = new java.net.URL("Enter your URL").openStream();
            bitmap = BitmapFactory.decodeStream(in);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    protected void onPostExecute(Bitmap image) {
         imageButton.setImageBitmap(image);      
    }
}

如果您想从本地目录或资源文件夹中设置图像,则只需从文件夹中获取图像并将其设置在图像中,而无需将其转换为位图。 谢谢

试试像这样简单的例子:

String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "jbn.jpg";
String pathName = baseDir + "/your/folder(s)/" +_ fileName; //maybe your folders are /DCIM/camera/

Bitmap bmp = BitmapFactory.decodeFile(pathName);
ImageButton button1 = (ImageButton)findViewById(R.id.imgBtn);
button1.setImageBitmap(bmp);