Android中保存图片到本地并更新到相册及图片出现在相册时间不正确解决方案

刚开始保存图片到本地更新相册后时间显示1970年月1日,网上看了好多方案都不用

详细代码:

/**
 * 将URL转化成bitmap形式
 *
 * @param url
 * @return bitmap type
 */
public final static Bitmap returnBitMap(String url) {
    URL myFileUrl;
    Bitmap bitmap = null;
    try {
        myFileUrl = new URL(url);
        HttpURLConnection conn;
        conn = (HttpURLConnection) myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream is = conn.getInputStream();
        bitmap = BitmapFactory.decodeStream(is);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bitmap;
}

 /**
     * 先保存到本地再广播到图库
     */
    public static void saveImageToGallery(Context context, Bitmap bmp, String fileName ,Handler mHandler) {
        // 首先保存图片
        final File appDir = new File(Environment.getExternalStorageDirectory(), "myQrcode");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
//        final String fileName = System.currentTimeMillis() + ".jpg";
        final File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
//        其次把文件插入到系统图库

        try {
            savePhotoToMedia(context,file,fileName);
            mHandler.obtainMessage(MyQrCodeActivity.SAVE_SUCCESS).sendToTarget();//保存成功
//            MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName,null);
//            // 最后通知图库更新
//            updatePhotoMedia(file,context);
//            context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,  Uri.parse("file://" + file)));//Uri.parse("file://" + file)
//            mHandler.obtainMessage(MyQrCodeActivity.SAVE_SUCCESS).sendToTarget();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            mHandler.obtainMessage(MyQrCodeActivity.SAVE_FAILURE).sendToTarget();//保存失败
            return;
        }
//        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file)));//Uri.parse("file://" + file)
//        mHandler.obtainMessage(MyQrCodeActivity.SAVE_SUCCESS).sendToTarget();
    }

 public static void savePhotoToMedia(Context context,File file,String fileName) throws FileNotFoundException {
        String uriString = MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
//        String uriString = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, null, null);
        File file1 = new File(getRealPathFromURI(Uri.parse(uriString),context));
        updatePhotoMedia(file1,context);
    }

    //更新图库
    private static void updatePhotoMedia(File file ,Context context){
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        intent.setData(Uri.fromFile(file));
        context.sendBroadcast(intent);
    }
    //得到绝对地址
    private static String getRealPathFromURI(Uri contentUri,Context context) {
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String fileStr = cursor.getString(column_index);
        cursor.close();
        return fileStr;
    }

你可能感兴趣的:(Android开发中的问题,Android开发)