【问题标题】:To store the image with date time stamp on sdcard将带有日期时间戳的图像存储在 sdcard 上
【发布时间】:2012-09-10 07:47:56
【问题描述】:

我已经编写了将位图图像存储在 sdcard 中的代码,但我想将它与日期时间戳一起存储。怎么做?

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

【问题讨论】:

    标签: android image datetime android-sdcard


    【解决方案1】:

    使用这个

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

    而不是

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

    试试这个:

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

    【讨论】:

    • SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dateTime = sdf.format(Calendar.getInstance().getTime());使用此代码我得到日期时间但是如何将其存储到图像
    • 而不是字符串 fname = "Image-"+ n +System.currentTimeMillis() +".jpg";使用 String fname = "Image-"+ n +getCurrentDate() +".jpg";
    • 当我这样做时,图像不会被存储
    • 您是否在 logcat 中收到任何警告?
    • 嗯,我想我明白了。文件名不应有 或 / 或 * 等检查您正在获取的文件的名称
    猜你喜欢
    • 2012-01-30
    • 2021-06-18
    • 2023-03-23
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-04
    • 2019-01-28
    相关资源
    最近更新 更多