Search code examples
androidimagedatetimeandroid-sdcard

To store the image with date time stamp on sdcard


I have Written the code to store the bitmap Image in sdcard But I want to store it along with the date time stamp .How to do it ?

bmp = processFrame(mCamera);
                String i1=bmp.toString();
                String root = Environment.getExternalStorageDirectory().toString();
                File myDir = new File(root + "/saved_images");    
                    myDir.mkdirs();
                    Random generator = new Random();
                    int n = 10000;
                    n = generator.nextInt(n);
                    String fname = "Image-"+ n +".jpg";
                    File file = new File (myDir, fname);
                    if (file.exists ()) file.delete (); 
                    try {
                           FileOutputStream out = new FileOutputStream(file);
                           bmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
                           out.flush();
                           out.close();

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

Solution

  • use this

    String fname = "Image-"+ n +System.currentTimeMillis() +".jpg";
    

    instead of

    String fname = "Image-"+ n +".jpg";
    

    try this :

    String getCurrentDate() {
            Calendar c = Calendar.getInstance();
            String Month = "";
            switch (c.get(Calendar.MONTH)) {
            case 0:
                  Month = "Jan";
                  break;
            case 1:
                  Month = "Feb";
                  break;
            case 2:
                  Month = "Mar";
                  break;
            case 3:
                  Month = "Apr";
                  break;
            case 4:
                  Month = "May";
                  break;
            case 5:
                  Month = "June";
                  break;
            case 6:
                  Month = "July";
                  break;
            case 7:
                  Month = "Aug";
                  break;
            case 8:
                  Month = "Sep";
                  break;
            case 9:
                  Month = "Oct";
                  break;
            case 10:
                  Month = "Nov";
                  break;
            case 11:
                  Month = "Dec";
                  break;
            default:
                  break;
            }
            int ampm = c.get(Calendar.AM_PM);
            String amorpm = "";
            if (ampm == 1) {
                  amorpm = "PM";
            } else {
                  amorpm = "AM";
            }
            String date = c.get(Calendar.DAY_OF_MONTH) + "/" + Month + "/"
                        + c.get(Calendar.YEAR) + " " + c.get(Calendar.HOUR) + ":"
                        + c.get(Calendar.MINUTE) + " " + amorpm;
            // int Day = c.get(Calendar.DAY_OF_MONTH);
            // int month = c.get(Calendar.MONTH);
            return date;
      }