刪除圖片/文件夾並通知系統刷新圖庫資源
//刪除文件後更新媒體庫 filepath(文件夾路徑)務必儘量精確
public static void updateFileFromDatabase(Context context,String filepath){
String where=MediaStore.Audio.Media.DATA+" like \""+filepath+"%"+"\"";
int i= context.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,where,null);
if(i>0){
Log.e(TAG,"媒體庫更新成功!");
}
}
----------------------------------------------------------------------------------------------------------------------------------------------------
拷貝圖片到指定文件夾返回絕對路徑/調整圖片方向
public class ImgUtils {
//保存文件到指定路徑
public static String saveImageToGallery(Context context, Bitmap bmp) {
// 首先保存圖片
String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "xczj";
File appDir = new File(storePath);
if (!appDir.exists()) {
appDir.mkdir();
}
String fileName = System.currentTimeMillis() + ".jpg";
File file = new File(appDir, fileName);
try {
FileOutputStream fos = new FileOutputStream(file);
//通過io流的方式來壓縮保存圖片
boolean isSuccess = bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);
fos.flush();
fos.close();
//把文件插入到系統圖庫
//MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
//保存圖片後發送廣播通知更新數據庫
Uri uri = Uri.fromFile(file);
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri));
if (isSuccess) {
return file.getAbsolutePath();
} else {
return "false";
}
} catch (IOException e) {
e.printStackTrace();
}
return "false";
}
/**
* @author yukaida
* @param absolutePath 照片的絕對路勁
* @return 重新調整方向之後的bitmap圖片
*/
public static Bitmap orientation(String absolutePath){
Bitmap bitmap_or= BitmapFactory.decodeFile(absolutePath);
try {
ExifInterface exif = new ExifInterface(absolutePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Log.d("EXIF", "Exif: " + orientation);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
}
else if (orientation == 3) {
matrix.postRotate(180);
}
else if (orientation == 8) {
matrix.postRotate(270);
}
bitmap_or= Bitmap.createBitmap(bitmap_or, 0, 0, bitmap_or.getWidth(), bitmap_or.getHeight(), matrix, true); // rotating bitmap
return bitmap_or;
}
catch (Exception e) {
}
return null;
}
}