作者:太2真人05 | 来源:互联网 | 2023-09-14 18:57
publicvoidsaveContentProvider(ResponseBodybody){ContentValuesvaluesnewContentValues();valu
public void saveContentProvider(ResponseBody body) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, destFileName);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/JPEG");
values.put(MediaStore.Images.Media.TITLE, String.valueOf(System.currentTimeMillis()).substring(0, 11) + ".jpg");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {//重要的看这里,这是Android 10以上的
values.put(MediaStore.Images.Media.RELATIVE_PATH, "DCIM/Camera");
} else { //这是以下的
values.put(MediaStore.Images.Media.DATA, Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath());
}
Uri external = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
ContentResolver resolver = Utils.getContext().getContentResolver();
Uri insertUri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {//这个在Android11上是成功的,有点奇怪
insertUri = resolver.insert(external, values);
} else {
insertUri = resolver.insert(external, new ContentValues());//重要的是这里10以下需要什么值都不设置,就成功了,然后设置了却没有成功,有点奇怪
}
OutputStream os = null;
InputStream in = null;
if (insertUri != null) {
try {
os = resolver.openOutputStream(insertUri);
in = body.byteStream();
Bitmap bitmap = BitmapFactory.decodeStream(in);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, os);
bitmap.recycle();
boolean isRecycled = bitmap.isRecycled();
if (isRecycled) {
Log.i("bitmap recycled :", " sucess");
} else {
Log.i("bitmap recycled :", " failed");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (os != null) {
os.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
因为写 的很急,很多都没有写,大概知道就好了