2

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();
              }
          }
      }
Jack
  • 1,825
  • 3
  • 26
  • 43
  • check this library out it helped me a lot...http://square.github.io/picasso/ – karan Jun 11 '15 at 05:28
  • Can you post your layout file? What's the size of your jpg? Have you added permission for reading file from file system? – km86 Jun 11 '15 at 05:51
  • @user3431672: i have added read permissions,please see my edit – Jack Jun 11 '15 at 06:11

4 Answers4

0

Try to get get AbsolutePath of image file :

File  imageFile = new File("/sdcard0/DCIM/camera/jibin.jpg");
Bitmap bmp = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
Haresh Chhelana
  • 24,720
  • 5
  • 57
  • 67
0

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);
    }
Sunny Garg
  • 1,073
  • 1
  • 6
  • 13
0

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

Jarvis
  • 503
  • 2
  • 5
  • 17
0

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);
Gabriella Angelova
  • 2,985
  • 1
  • 17
  • 30
  • you could use the following algorithm : 1.get all files from the camera folder; 2. get the date of the creation of the files like this: http://stackoverflow.com/questions/2389225/android-how-to-get-a-files-creation-date and 3.get the newest file as resource – Gabriella Angelova Jun 11 '15 at 06:32
  • Just a moment, I will write you a simple example – Gabriella Angelova Jun 11 '15 at 07:25
  • Here is what I am talking about: http://pastebin.com/TMWAqDd1 I think that you should iterate trough all the files in the directory to see which is the newest file, because it is not sure that the files are in alphabetical order. P.S. I just saw that I make a mistake on line 13 -> it is `file.lastModified()` with () – Gabriella Angelova Jun 11 '15 at 07:34
  • Is there any modifications needed on this line of code if (lastModDate.after(strDate)) line numbr 18 ,strDate function is not working,it showing an error.And Thank u very much for the effort u hav taken. – Jack Jun 11 '15 at 08:23
  • Ohhh, sorry.. this `if (lastModDate.after(strDate)) {` should be `if (lastModDate.after(lastDate)) {` it's my mistake. Sorry once again. Try it this way – Gabriella Angelova Jun 11 '15 at 11:14
  • what error do you become? I thought that the error was, that strDate doesn't exist, because I have mistaken lastDate with strDate at the beginning, but maybe there is something else ? – Gabriella Angelova Jun 11 '15 at 11:40
  • You're welcome :) My linkedIn name is Gabriela Angelova (with single "l"). If you need link or what else, just write me – Gabriella Angelova Jun 12 '15 at 05:53
  • Hi, I replied to your connections' invitation :) – Gabriella Angelova Jun 15 '15 at 09:12