我想从图库中获取图像并将其保存到另一个文件夹。我尝试使用代码
private void copyFile(File inputPath, File outputPath){
try{
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(inputPath);
out = new FileOutputStream(outputPath);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file (You have now copied the file)
out.flush();
out.close();
out = null;
Log.d("Copied file to ", outputPath.toString());
} catch (FileNotFoundException fnfe1) {
Log.d("Tag",fnfe1.getMessage());
} catch (Exception e) {
Log.d("tag", e.getMessage());
}
}catch (Exception e){
e.printStackTrace();
}
}
最佳答案
我建议使用 try-with-resources 和 java 8 Files/Path api:
private void copyFile(File inputPath, File outputPath){
try (FileOutputStream fos = new FileOutputStream(outputPath)) {
Files.copy(inputPath.toPath(), fos);
Log.d("Copied file to ", outputPath.toString());
} catch (FileNotFoundException fnfe1) {
Log.d("Tag",fnfe1.getMessage());
} catch (Exception e) {
Log.d("tag", e.getMessage());
}
}
关于java - 如何将图像文件从图库移动到另一个文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57426598/