1970的时间问题,貌似是咱们大多数人在保存图片时都会遇到的问题,那么,今天就来说一下这个问题,这也是几天我刚解决好的,所以拿出来跟大家分享一下,如果大家有更加简洁的方法,及时提出,咱们共同进步
第一步:下载
切记,要先请求权限,在权限允许的情况下,调用下载,方法也可以用于多图片下载。
//请求权限
getRxPermissions().request(Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(Boolean aBoolean) throws Exception {
if (aBoolean) { //如果权限允许
if (selectList.size() == 0) {
RxToast.warning("请选择你要分享的图片");
return;
}
//这是for循环,对于多图片下载,可以使用
for (int i = 0; i < selectList.size(); i++) {
//获取图片链接
picUrl = selectList.get(i);
// 下载图片到本地
DownPicUtil.downPic(picUrl, new DownPicUtil.DownFinishListener() {
@Override
public void getDownPath(String s) {
Message msg = Message.obtain();
msg.obj = s;
handler.sendMessage(msg);
}
});
}
} else {
RxToast.warning("权限被拒,请打开权限在保存图片");
}
}
});
其次就是下载的demo:
/**
* 图片下载的工具类
*/
public class DownPicUtil {
/**
* 下载图片,返回图片的地址
* 参数
* url 图片下载地址
* downFinishListener 回调下载结果
*
* @param url
*/
public static void downPic(String url, DownFinishListener downFinishListener) {
// 获取存储卡的目录
String filePath = Environment.getExternalStorageDirectory().getPath();
File file = new File(filePath + File.separator);
if (!file.exists()) {
file.mkdir();
}
loadPic(file.getPath(), url, downFinishListener);
}
private static void loadPic(final String filePath, final String url, final DownFinishListener downFinishListener) {
new AsyncTask<Void, Void, String>() {
String fileName;
InputStream is;
OutputStream out;
@Override
protected String doInBackground(Void... voids) {
// 下载文件的名称
String[] split = url.split("/");
fileName = split[split.length - 1];
// 创建目标文件,不是文件夹
File picFile = new File(filePath + File.separator + fileName);
if (picFile.exists()) {
return picFile.getPath();
}
try {
URL picUrl = new URL(url);
//通过图片的链接打开输入流
is = picUrl.openStream();
if (is == null) {
return null;
}
out = new FileOutputStream(picFile);
byte[] b = new byte[1024];
int end;
while ((end = is.read(b)) != -1) {
out.write(b, 0, end);
}
if (is != null) {
is.close();
}
if (out != null) {
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return picFile.getPath();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s != null) {
downFinishListener.getDownPath(s);
}
}
}.execute();
}
//下载完成回调的接口
public interface DownFinishListener {
void getDownPath(String s);
}
}
第二步:切换到主线程
切换到主线程异常使用的就是Handler:
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String picFile = (String) msg.obj;
String[] split = picFile.split("/");
String fileName = split[split.length - 1];
//正确修改图片详情显示时间
String insertImage = null;
try {
MediaStore.Images.Media.insertImage(XApplication.getContext().getContentResolver(), picFile, fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最后通知图库更新
XApplication.getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + picFile)));
Toast.makeText(MyApplication.getContext(), "图片保存图库成功", Toast.LENGTH_LONG).show();
}
};
这就是我们常用的更新图库的方法,但是这样通常就会出现1970,时间错误的现象,要想解决这个问题,我们就要在图库更新的时候对其文件进行时间的设置:
第三步:更新图库
代码如下:
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
String picFile = (String) msg.obj;
String[] split = picFile.split("/");
String fileName = split[split.length - 1];
//正确修改图片详情显示时间
String insertImage = null;
try {
insertImage = MediaStore.Images.Media.insertImage(context.getContentResolver(), picFile, fileName, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
File file1 = new File(getRealPathFromURI(Uri.parse(insertImage), context));
updatePhotoMedia(file1, context);
Toast.makeText(MyApplication.getContext(), "图片保存图库成功", Toast.LENGTH_LONG).show();
}
};
//更新图库
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;
}
修改完毕,这样就不会出现,图片时间错误的问题了,