import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View; public class PictureUtils { private static String TAG = PictureUtils.class.getSimpleName(); private static int CODE_WIDTH = 60;
private static int CODE_HEIGHT = 60; private PictureUtils() { } /**
* 保存一张或者多张图片到图库
*/
public static void saveImageListToGallery(final BaseActivity context,
final List<String> imgList, final OnSaveImgCallBack callBack) { if (context == null) {
LogUtils.dTag(TAG, "context 不能为null");
return;
} RxPermissionsUtils.getInstance()
.requestPermission(context, RxPermissionsUtils.READ_AND_WRITE_PERMISSION,
new RxPermissionsUtils.OnPermissionRequestCallBack() {
@Override
public void onSuccess() {
if (imgList == null) {
LogUtils.dTag(TAG, "图片数组为 null");
return;
} // 保存图片到图库
saveImageListToGalleryPrivate(context, imgList, callBack); } @Override
public void onFail() {
ToastUtils.showShort("请在设置授予应用存储权限^_^");
PermissionUtils.launchAppDetailsSettings();
}
}); } /**
* 保存多张图片
*/
private static void saveImageListToGalleryPrivate(BaseActivity context, List<String> imgList,
final OnSaveImgCallBack callBack) { Observable.just(imgList)
.observeOn(Schedulers.io())
.flatMap(new Function<List<String>, ObservableSource<Boolean>>() {
@Override
public ObservableSource<Boolean> apply(List<String> strings) throws Exception { for (String filePath : strings) { if (filePath.startsWith("http://") || filePath.startsWith("https://")) { Bitmap bmp = Glide.with(BaseApplication.getInstance())
.asBitmap()
.load(filePath)
.submit()
.get(); saveImageToGallery(BaseApplication.getInstance(), bmp, filePath); } else { Bitmap bmp = BitmapFactory.decodeFile(filePath); saveImageToGallery(BaseApplication.getInstance(), bmp, filePath);
} } return Observable.just(true);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Boolean>() {
Disposable mD; @Override
public void onSubscribe(Disposable d) {
mD = d;
if (callBack != null) {
callBack.onSaveStart();
}
} @Override
public void onNext(Boolean aBoolean) {
ToastUtils.showShort("图片已成功保存到系统相册^_^");
if (callBack != null) {
callBack.onSaveSuccess();
}
} @Override
public void onError(Throwable e) {
if (mD != null) {
mD.dispose();
}
if (callBack != null) {
callBack.onSaveFail();
}
} @Override
public void onComplete() {
if (mD != null) {
mD.dispose();
}
}
}); } /**
* 保存 Bitmap 到相册 获得路径
*/
public static String saveImageToGallery(Context context, Bitmap bmp, String imageUrl) { if (!SDCardUtils.isSDCardEnableByEnvironment()) {
LogUtils.dTag(TAG, "内存卡不可用");
return null;
} // 首先保存图片
File appDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
NetWorkConstants.FileKey.COMMONLIB_SHARE_CODE_DIR_NAME);// 获取文件存储路径 if (!appDir.exists()) {
boolean isCreate = appDir.mkdir();
if (!isCreate) {
return null;
}
} // 创建文件 截取,取出名字
String url_truncated = String.valueOf(System.currentTimeMillis());
String fileName = "mryt_share_code" + "_" + url_truncated;
File file = new File(appDir, fileName);
FileUtils.deleteFile(file);
if (FileUtils.isFileExists(file)) {
LogUtils.dTag(TAG, file.getAbsolutePath());
return file.getAbsolutePath();
}
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
// 插入图库
/**MediaStore.Images.Media
.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName,
String.valueOf(System.currentTimeMillis()));*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 最后通知图库更新 /**MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override
public void onScanCompleted(String path, Uri uri) { }
});*/
try {
savePhotoToMedia(context, file, fileName);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return file.getAbsolutePath();
} /**
* 保存 Bitmap 到相册 获得路径 不传递文件名称 用时间后缀
*/
public static String saveImageToGallery(Context context, Bitmap bmp) {
// 首先保存图片
File appDir = context.getExternalFilesDir(
NetWorkConstants.FileKey.COMMONLIB_SHARE_CODE_DIR_NAME);// 获取文件存储路径
if (appDir != null && !appDir.exists()) {
boolean isCreate = appDir.mkdir();
if (!isCreate) {
return null;
}
}
if (!SDCardUtils.isSDCardEnableByEnvironment()) {
LogUtils.dTag(TAG, "内存卡不可用");
return null;
} // 创建文件 截取,取出名字
String url_truncated = String.valueOf(System.currentTimeMillis());
String fileName = "mryt_share_code" + "_" + url_truncated;
File file = new File(appDir, fileName);
if (FileUtils.isFileExists(file)) {
LogUtils.dTag(TAG, file.getAbsolutePath());
return file.getAbsolutePath();
}
try {
FileOutputStream fos = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
// 插入图库
/**MediaStore.Images.Media
.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName,
String.valueOf(System.currentTimeMillis()));*/
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 最后通知图库更新
// context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
/**MediaScannerConnection.scanFile(context, new String[]{file.getAbsolutePath()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
@Override public void onScanCompleted(String path, Uri uri) {
}
});*/ try {
savePhotoToMedia(context, file, fileName);
ToastUtils.showShort("图片已成功保存到相册");
} catch (FileNotFoundException e) {
e.printStackTrace();
ToastUtils.showShort("图片保存失败");
}
return file.getAbsolutePath();
} public static Bitmap getBitmapFromView(Context context, View view) {
view.setDrawingCacheEnabled(true);
//启用DrawingCache并创建位图
view.buildDrawingCache();
//创建一个DrawingCache的拷贝,因为DrawingCache得到的位图在禁用后会被回收
Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
//禁用DrawingCahce否则会影响性能
view.setDrawingCacheEnabled(false);
return bitmap;
} //图片上绘制文字
public static Bitmap drawTextToBitmap(Bitmap bitmap, String text,
Paint paint, Rect bounds, int paddingLeft, int paddingTop) {
android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig(); paint.setDither(true); // 获取跟清晰的图像采样
paint.setFilterBitmap(true);// 过滤一些
if (bitmapConfig == null) {
bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
}
bitmap = bitmap.copy(bitmapConfig, true);
Canvas canvas = new Canvas(bitmap);
canvas.drawText(text, paddingLeft, paddingTop, paint);
return bitmap;
} public static Bitmap setImgSize(Bitmap bm, int newWidth, int newHeight) {
// 获得图片的宽高.
int width = bm.getWidth();
int height = bm.getHeight();
// 计算缩放比例.
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 取得想要缩放的matrix参数.
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的图片.
return Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true);
} // 保存图片的回调
public interface OnSaveImgCallBack { void onSaveStart(); void onSaveSuccess(); void onSaveFail();
} 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);
if (cursor == null) {
return "";
}
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String fileStr = cursor.getString(column_index);
cursor.close();
return fileStr;
}
}

Android--解决图片保存到相册显示1970年1月1日 8:00的问题的更多相关文章

  1. 1970年1月1日(00:00:00 GMT)Unix 时间戳(Unix Timestamp)

    转载自(http://jm.ncxyol.com/post-88.html) 今天在看Python   API时,看到time模块: The epoch is the point where the ...

  2. Android将图片保存到相册并及时看到

    Android中将图片保存到SD卡中,相册里不会及时出现这张图片,因为没有及时更新其索引,一般需要开机几次.当然我们可以手动更新其索引. 1,首先将文件保存到SD卡中. String filePath ...

  3. [RN] React Native 图片保存到相册(支持 Android 和 ios)

    React Native 图片保存到相册(支持 Android 和 ios) 原理: IOS用 RN自带的 CameraRoll, Android 使用 不成功,需要 react-native-fs  ...

  4. 小程序图片保存的相册授权失败问题,原接口wx.openSeting()已经废弃了的解决办法

    项目中使用的是wepy框架开发的小程序,,, 使用场景是,用户点击下载图片的时候,要调起授权button(小程序拉起授权的功能都集成在了button组件,所以这里需要用到button组件里的一个ope ...

  5. java为啥计算时间从1970年1月1日开始

    http://www.myexception.cn/program/1494616.html ————————————————————————————————————————————————————— ...

  6. Java 获取各时区时间,获取当前时间到格林威治时间1970年01月01日00时00分00秒的秒数

    格林威治时间即UTC/GMT时间,1970年01月01日00时00分00秒(即UTC+8的北京时间1970年01月01日08时00分00秒)计算代码如下: /** * 获取指定时间到格林威治时间的秒数 ...

  7. 从1970年1月1日00:00:00 GMT以来此时间对象表示的毫秒数转化为Datetime

    1970年1月1日(00:00:00 GMT)Unix 时间戳(Unix Timestamp)对时间转换 将Long类型转换为DateTime类型 /// <summary> /// 将L ...

  8. 格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数。

    格林治时间,也就是返回从 UTC 1970 年 1 月 1 日午夜开始经过的毫秒数. (* Delphi获取13位格林治时间实现方法, 与java中的java.lang.System.currentT ...

  9. DedeCMS文章编辑不更新时间1970年1月1日

    在修改文章或者后期优化的时候,织梦dedecms5.7版本存在一个问题,修改文章的同时也修改了文章的发布时间,这个功能可能有些人比较需要,但同时也有些站长朋友又不需要,因为我们编辑某个文章的时候,发现 ...

随机推荐

  1. [Swift]LeetCode39. 组合总和 | Combination Sum

    Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), fin ...

  2. [Swift]LeetCode214. 最短回文串 | Shortest Palindrome

    Given a string s, you are allowed to convert it to a palindrome by adding characters in front of it. ...

  3. [Swift]LeetCode417. 太平洋大西洋水流问题 | Pacific Atlantic Water Flow

    Given an m x n matrix of non-negative integers representing the height of each unit cell in a contin ...

  4. [Swift]LeetCode763. 划分字母区间 | Partition Labels

    A string S of lowercase letters is given. We want to partition this string into as many parts as pos ...

  5. [Swift]LeetCode907. 子数组的最小值之和 | Sum of Subarray Minimums

    Given an array of integers A, find the sum of min(B), where B ranges over every (contiguous) subarra ...

  6. Go语言的一些使用心得

    序 起初一直使用的Python,到了18年下半年由于业务需求而接触了Golang,从开始学习到现在的快半年里,也用Golang写了些代码,公司产品和业余写的都有,今天就写点Golang相关的总结或者感 ...

  7. java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.paipaixiu/com.example.paipaixiu.MASetSomeThing}: android.view.InflateException: Binary XML file line #19: Attempt to invo

    这个报错是因为Android布局的控件 <view android:layout_width="match_parent" android:layout_height=&qu ...

  8. AspNetCore taghelpers标签的使用

    下面介绍几种常用的Tag标签 asp-for 类似于name asp-validation-for 类似于mvc下的验证,将的验证来源于model的验证特性 asp-validation-summar ...

  9. 从锅炉工到AI专家(10)

    RNN循环神经网络(Recurrent Neural Network) 如同word2vec中提到的,很多数据的原型,前后之间是存在关联性的.关联性的打破必然造成关键指征的丢失,从而在后续的训练和预测 ...

  10. 【Java基础】【01初识Java】

    01.01_计算机基础知识(计算机概述)(了解) A:什么是计算机?计算机在生活中的应用举例 计算机(Computer)全称:电子计算机,俗称电脑.是一种能够按照程序运行,自动.高速处理海量数据的现代 ...