How to set Image on ImageButton dynamically?

3.4k views Asked by At

I want to set an image on a button in my app, dynamically from a file on the sdcard. I have tried this code but it is not working. I have tried to convert the image to a bitmap object and I set that object to ImageButton, but it isn't showing anything. How can I solve this issue?

My code:

@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"
   />

Algorithm

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

There are 4 answers

8
Gabriella Angelova On BEST ANSWER

Try with something simple like this for example:

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);
0
Haresh Chhelana On

Try to get get AbsolutePath of image file :

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

You can try something like this -

    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);
    }
0
Jarvis On

If you want to set the image dynamically from any URL that you have, set the image in this way. You can also set the bitmap width and height.

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

And if you want to set the image from the local directory or from the resources folder then just fetch the Image from the folder and set that in image and you don't need to convert it into a Bitmap. Thanks