如何动态设置ImageButton上的Image?

时间:2015-06-11 05:27:31

标签: android android-bitmap android-imagebutton

我想在我的应用程序中的按钮上设置图像,动态地从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();
              }
          }
      }

4 个答案:

答案 0 :(得分:0)

尝试获取图像文件的AbsolutePath:

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

答案 1 :(得分:0)

你可以尝试这样的事情 -

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

答案 2 :(得分:0)

如果要从任何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);      
    }
}

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

答案 3 :(得分:0)

尝试使用这样简单的东西,例如:

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