val downloadDir = File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), getString(R.string.app_name) )if (!downloadDir.isDirectory) { //Creates directory named by this file downloadDir.mkdirs() } val file = File(downloadDir, fileName) try { val ostream = FileOutputStream(file) ostream.write(imageByteArray) ostream.flush() ostream.close() }
答案 0 :(得分:0)
我正在使用以下代码在sdcard
中保存用户的头像。
您可以更改代码以适合您的需求:
public static String copyToAppDirectory(Context c, Uri uri, String personID) throws IOException {
File src = new File(uri.getPath());
File mediaStorageDir = new File(Environment.getExternalStorageDirectory(), BaseConst.AppDir);
mediaStorageDir = new File(mediaStorageDir, BaseConst.ImageFolder);
File dst = new File(mediaStorageDir, personID + ".JPG");
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
throw new IOException("Cannot create Directory!");
}
}
InputStream in;
try {
in = c.getContentResolver().openInputStream(uri);
} catch (Exception ex) {
in = new FileInputStream(src);
}
try {
OutputStream out = new FileOutputStream(dst);
try {
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
} finally {
out.close();
}
} finally {
in.close();
}
return dst.getPath();
}
希望对您有帮助