保存图片到图库更新图库

Bitmap image = ((BitmapDrawable) iv.getDrawable()).getBitmap();

if (!TextUtils.isEmpty(pics[vpPicNine.getCurrentItem()])) {
String[] sts = TextUtils.convertStr(pics[vpPicNine.getCurrentItem()]);
PicUtils.saveImageToGallery(HuPicLunBoActivity.this, image, sts[sts.length - 1]);
} else {
ToastUtils.showSnackBar(snackView, "保存失败");

}



/**
* 参数一:上下文
* 参数二:图片
* 参数三:网络地址最后一部分;
* @param context
* @param bmp
* @param fileNa1me
*/

public static void saveImageToGallery(Context context, Bitmap bmp,
String fileNa1me) {
if (bmp == null) {
Toast.makeText(context, "保存失败", Toast.LENGTH_SHORT).show();
return;
}
// 首先保存图片
File appDir = new File(Environment.getExternalStorageDirectory(),
"DeskTop/photo");
if (deleteLastFromFloder(appDir.toString(), fileNa1me)) {
//图片以保存
Toast.makeText(context, "已保存", Toast.LENGTH_SHORT).show();
return;
}

if (!appDir.exists()) {
appDir.mkdirs();//创建文件夹
}
File file = new File(appDir, fileNa1me);
String string = file.getPath();
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(context, "保存成功", Toast.LENGTH_SHORT).show();
L.d("aaa", "文件地址" + string);
FileOutputStream out;
try {
out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);

out.flush();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

// // 其次把文件插入到系统图库
try {
MediaStore.Images.Media.insertImage(context.getContentResolver(),
file.getAbsolutePath(), string, null);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 最后通知图库更新
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,
Uri.parse("file://" + "DeskTop/photo")));
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
MediaScannerConnection.scanFile(context,
new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {

}
});
}





/**
*遍历该文件下的文件名
*/

public static boolean deleteLastFromFloder(String path, String strPath) {
boolean success = false;
try {
ArrayList<File> images = new ArrayList<File>();
getFiles(images, path);
File latestSavedImage = images.get(0);
if (latestSavedImage.exists()) {
for (int i = 1; i < images.size(); i++) {
File nextFile = images.get(i);
if (TextUtils.isString(strPath.trim(), nextFile.toString().trim())) {
L.d("aaa", "图片文件名称" + nextFile.toString());
return true;
}

}
}
} catch (Exception e) {
e.printStackTrace();
}
return success;
}

public static void getFiles(ArrayList<File> fileList, String path) {
File[] allFiles = new File(path).listFiles();
for (int i = 0; i < allFiles.length; i++) {
File file = allFiles[i];
if (file.isFile()) {
fileList.add(file);
} else if (!file.getAbsolutePath().contains(".thumnail")) {
getFiles(fileList, file.getAbsolutePath());
}
}
}






/**
* 某个字段是否包含该字符
*
* @param questId 短字符
* @param str 长字符
* @return
*/
public static boolean isString(String questId, String str) {

if (str.indexOf(questId) != -1) {
return true;
}
return false;

}

/**
* /字符串 得到字符数组
*
* @param str
* @return
*/
public static String[] convertStr(String str) {
String[] strArray;
strArray = str.split("/"); //拆分字符为"," ,然后把结果交给数组strArray
return strArray;
}












原文地址:https://www.cnblogs.com/huihuizhang/p/6137994.html