从Gallery返回的日期始终返回1970年1月1日

时间:2015-09-16 05:52:06

标签: android android-gallery

我正在尝试构建一个图库应用。我已经从MediaStore.Images获取了图片路径和日期。

我使用了以下代码:

public static ArrayList<String> getAllShownImagesPath(Activity activity) {
        Uri uri;
        Cursor cursor;
        int column_index_data, column_index_folder_name;
        int column_index_date;
        ArrayList<String> listOfAllImages = new ArrayList<String>();
        String absolutePathOfImage = null;
        long dateofimage;
        uri = android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        String[] projection = { MediaColumns.DATA,MediaColumns.DATE_ADDED,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME };

        cursor = activity.getContentResolver().query(uri, projection, null,
                null, null);

        column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
        column_index_date=cursor.getColumnIndex(MediaColumns.DATE_ADDED);
        column_index_folder_name = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
        while (cursor.moveToNext()) {
            absolutePathOfImage = cursor.getString(column_index_data);
            System.out.println("Imagepath-->"+absolutePathOfImage);

            dateofimage = cursor.getLong(column_index_date);
            System.out.println("date--->"+dateofimage);


            Date d = new Date(dateofimage); 
            java.text.DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy h:mmaa"); 
            String dateString = formatter.format(d);

            //String  dateString= convertDate(dateofimage,"dd/MM/yyyy hh:mm:ss");

            System.out.println("date in format--->"+dateString);
            listOfAllImages.add(absolutePathOfImage);
        }
        return listOfAllImages;
    }

请参阅我在下面发布的logcat。图像路径完美,以毫秒为单位的时间。但是当我尝试将毫秒转换为日期格式时,我总是得到: 1970年1月17日

09-16 11:10:53.994: I/System.out(10098): date in format--->January 17, 1970 8:49PM
    09-16 11:10:53.994: I/System.out(10098): Imagepath-->/storage/emulated/0/ModFace/Modface727142626.jpg
    09-16 11:10:53.994: I/System.out(10098): date--->1437988950
    09-16 11:10:53.994: I/System.out(10098): date in format--->January 17, 1970 8:56PM
    09-16 11:10:53.994: I/System.out(10098): Imagepath-->/storage/emulated/0/ModFace/Modface727142639.jpg
    09-16 11:10:53.994: I/System.out(10098): date--->1437988950
    09-16 11:10:53.994: I/System.out(10098): date in format--->January 17, 1970 8:56PM
    09-16 11:10:53.994: I/System.out(10098): Imagepath-->/storage/emulated/0/data/data/images/SILogo2015.png
    09-16 11:10:53.994: I/System.out(10098): date--->1438252547
    09-16 11:10:53.994: I/System.out(10098): date in format--->January 17, 1970 9:00PM
    09-16 11:10:53.994: I/System.out(10098): Imagepath-->/storage/emulated/0/Download/ebook.png
    09-16 11:10:53.994: I/System.out(10098): date--->1438668694
    09-16 11:10:53.994: I/System.out(10098): date in format--->January 17, 1970 9:07PM
    09-16 11:10:53.994: I/System.out(10098): Imagepath-->/storage/emulated/0/DCIM/Camera/20150811_153514.jpg
    09-16 11:10:53.994: I/System.out(10098): date--->1439287514
    09-16 11:10:53.994: I/System.out(10098): date in format--->January 17, 1970 9:18PM
    09-16 11:10:53.994: I/System.out(10098): Imagepath-->/storage/emulated/0/Pictures/mainstreamtemp.jpg
    09-16 11:10:53.994: I/System.out(10098): date--->1439800096
    09-16 11:10:53.994: I/System.out(10098): date in format--->January 17, 1970 9:26PM
    09-16 11:10:53.994: I/System.out(10098): Imagepath-->/storage/emulated/0/Pictures/mainstream2.jpg
    09-16 11:10:53.994: I/System.out(10098): date--->1439800096
    09-16 11:10:53.994: I/System.out(10098): date in format--->January 17, 1970 9:26PM
    09-16 11:10:54.024: I/System.out(10098): date in format--->January 17, 1970 10:01PM
    09-16 11:10:54.024: I/System.out(10098): Imagepath-->/storage/emulated/0/shiatoolkitimages/mashlool07.gif
    09-16 11:10:54.024: I/System.out(10098): date--->1441888058
    09-16 11:10:54.024: I/System.out(10098): date in format--->January 17, 1970 10:01PM
    09-16 11:10:54.024: I/System.out(10098): Imagepath-->/storage/emulated/0/shiatoolkitimages/mashlool08.gif
    09-16 11:10:54.024: I/System.out(10098): date--->1441888058
    09-16 11:10:54.024: I/System.out(10098): date in format--->January 17, 1970 10:01PM
    09-16 11:10:54.024: I/System.out(10098): Imagepath-->/storage/emulated/0/shiatoolkitimages/mashlool09.gif
    09-16 11:10:54.024: I/System.out(10098): date--->1441888058
    09-16 11:10:54.024: I/System.out(10098): Imagepath-->/storage/emulated/0/shiatoolkitimages/mashlool10.gif
    09-16 11:10:54.024: I/System.out(10098): date--->1441888058
    09-16 11:10:54.024: I/System.out(10098): Imagepath-->/storage/emulated/0/shiatoolkitimages/mashlool11.gif
    09-16 11:10:54.024: I/System.out(10098): date in format--->January 17, 1970 10:01PM
    09-16 11:10:54.024: I/System.out(10098): Imagepath-->/storage/emulated/0/siat_hadith.png
    09-16 11:10:54.024: I/System.out(10098): date--->1442042302
    09-16 11:10:54.024: I/System.out(10098): date in format--->January 17, 1970 10:04PM

我还尝试了另一种方法来转换millis的日期,但最终获得了相同的结果:

public static String convertDate(long dateInMilliseconds,String dateFormat) {
        return DateFormat.format(dateFormat, dateInMilliseconds).toString();
    }

2 个答案:

答案 0 :(得分:5)

您从光标获得的数字格式为Unix Time,它计算自1970年1月1日以来经过的秒数。对于您的日期创建,您需要以毫秒为单位的值。简单的解决方案是将结果乘以1000,你就可以了。

答案 1 :(得分:3)

而不是这个

         Date d = new Date(dateofimage); 
        java.text.DateFormat formatter = new SimpleDateFormat("MMMM dd, yyyy h:mmaa"); 
        String dateString = formatter.format(d);   

试试这个

        Timestamp timeStamp = new Timestamp(dateofimage * 1000);
        String dateString = timeStamp.toLocaleString();