牙叔教程 简单易学
使用场景
下载图片后, 将图片信息刷新到图库
代码讲解
第一种
media.scanFile(filePath);
第二种
app.sendBroadcast(
new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, android.net.Uri.fromFile(new java.io.File(filePath)))
);
第三种
// 分享者: @七桶
function scanFile(filePath) {
android.provider.MediaStore.Images.Media.insertImage(
context.getContentResolver(),
files.path(filePath),
files.getNameWithoutExtension(filePath),
null
);
let intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
let file = new java.io.File(filePath);
let uri = android.net.Uri.fromFile(file);
intent.setData(uri);
context.sendBroadcast(intent);
}
第四种
function scanFile(filePath) {
log(filePath);
var filePathArr = java.lang.reflect.Array.newInstance(java.lang.Class.forName("java.lang.String"), 1);
filePathArr[0] = java.lang.String(filePath);
android.media.MediaScannerConnection.scanFile(
context,
filePathArr,
null,
new android.media.MediaScannerConnection.OnScanCompletedListener({
onScanCompleted: function (path, uri) {
log("onScanCompleted");
log("*******", "Scanned " + path + ":");
log("*******", "-> uri=" + uri);
},
})
);
}
第五种
// 分享者: @Tho
function saveToMediaStore(img, filePath) {
// 系统时间
let mImageTime = new Date().getTime();
let dateSeconds = mImageTime / 1000;
// 文件名
let mImageFileName = files.getName(filePath);
// 文件路径
let mImageFilePath = files.path(filePath);
let file = new java.io.File(mImageFilePath);
let resolver = context.getContentResolver();
if (file.exists()) {
files.remove(mImageFilePath);
let params = util.java.array("string", 1);
params[0] = mImageFilePath;
resolver.delete(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
android.provider.MediaStore.Images.Media.DATA + " LIKE ?",
params
);
}
let mImageHeight = img.getHeight();
let mImageWidth = img.getWidth();
// 保存截屏到系统MediaStore
let values = new android.content.ContentValues();
values.put(android.provider.MediaStore.Images.ImageColumns.DATA, mImageFilePath);
values.put(android.provider.MediaStore.Images.ImageColumns.TITLE, mImageFileName);
values.put(android.provider.MediaStore.Images.ImageColumns.DISPLAY_NAME, mImageFileName);
values.put(android.provider.MediaStore.Images.ImageColumns.DATE_TAKEN, new java.lang.Long(mImageTime));
values.put(android.provider.MediaStore.Images.ImageColumns.DATE_ADDED, new java.lang.Double(dateSeconds));
values.put(android.provider.MediaStore.Images.ImageColumns.DATE_MODIFIED, new java.lang.Double(dateSeconds));
values.put(android.provider.MediaStore.Images.ImageColumns.MIME_TYPE, "image/png");
values.put(android.provider.MediaStore.Images.ImageColumns.WIDTH, new java.lang.Long(mImageWidth));
values.put(android.provider.MediaStore.Images.ImageColumns.HEIGHT, new java.lang.Long(mImageHeight));
let uri = resolver.insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
images.save(img, mImageFilePath);
values.clear();
values.put(android.provider.MediaStore.Images.ImageColumns.SIZE, new File(mImageFilePath).length());
resolver.update(uri, values, null, null);
}
声明
部分内容来自网络
微信公众号 牙叔教程
QQ群
747748653