我想将图像文件从一个位置复制到另一个位置,我的图像文件存储在数据
在Android模拟器中的文件夹,我想在数据文件夹的子文件夹中复制或移动它。
为此,我使用了
File,FileInputStream和FileOutputStream。现在我无法理解如何提供
的路径存储在android模拟器中的文件。请帮我解决这个问题。
答案 0 :(得分:0)
尝试将其仅用于图像..
private File imageFile;
private boolean writeFile() {
imageFile = new File(
getApplicationContext().getFilesDir() + "/images/",
"sample.png");
/*
Image save to "/data/data/com.mycomp.android.test/files/images/sample.png"
com.mycomp.android.test is your package name
*/
String savePath = imageFile.getAbsolutePath();
System.out.println("savePath :" + savePath + ":");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(savePath, false);
} catch (FileNotFoundException ex) {
String parentName = new File(savePath).getParent();
if (parentName != null) {
File parentDir = new File(parentName);
if ((!(parentDir.exists())) && (parentDir.mkdirs()))
try {
fileOutputStream = new FileOutputStream(savePath, false);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
/*
path which image you need to move on data folder
*/
String path = Environment.getExternalStorageDirectory()
+ "/amit/amit.png";
File imgFile = new File(path);
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imgBuffer = stream.toByteArray();
boolean result = true;
try {
fileOutputStream.write(imgBuffer);
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
result = false;
} catch (IOException e2) {
e2.printStackTrace();
result = false;
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
result = false;
}
}
}
return result;
}