内部存储路径Uri
- 图片:MediaStore.Images.Media.INTERNAL_CONTENT_URI
- 音频:MediaStore.Audio.Media.INTERNAL_CONTENT_URI
- 视频:MediaStore.Video.Media.INTERNAL_CONTENT_URI
- 下载:MediaStore.Downloads.INTERNAL_CONTENT_URI
文件复制
String fileName = srcFile.getName()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver cr = context.getContentResolver()
ContentValues cv = new ContentValues()
cv.put(MediaStore.Images.Media.DESCRIPTION, fileName)
cv.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/Kidney")
cv.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg")
//insert会自动更新相册
Uri uri = cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, cv)
InputStream is = null
OutputStream os = null
try {
is = new FileInputStream(srcFile)
if (uri != null) {
os = cr.openOutputStream(uri)
}
if (os != null) {
byte[] buffer = new byte[1024]
int byteCount
while ((byteCount = is.read(buffer)) > 0) {
os.write(buffer, 0, byteCount)
}
}
} catch (IOException e) {
e.printStackTrace()
} finally {
try {
if (is != null)
is.close()
if (os != null)
os.close()
} catch (IOException e) {
e.printStackTrace()
}
}
} else {
FileInputStream fis = null
FileOutputStream fos = null
try {
String path = SdUtils.getPicturesCustomPath("Kidney")
File targetFile = new File(path, fileName)
fis = new FileInputStream(srcFile)
fos = new FileOutputStream(targetFile)
byte[] buffer = new byte[1024]
int byteCount
while ((byteCount = fis.read(buffer)) > 0) {
fos.write(buffer, 0, byteCount)
}
// 实现相册更新
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE)
Uri uri = Uri.fromFile(targetFile)
intent.setData(uri)
context.sendBroadcast(intent)
} catch (IOException e) {
e.printStackTrace()
} finally {
try {
if (fis != null)
fis.close()
if (fos != null)
fos.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}