我到目前为止编写的代码如下:
String fname = type + (i + 1) + "-" + ext;
String fname1 = type + (i + 2) + "-" + System.currentTimeMillis() + ext;
File file = new File(myDir, fname);
if (file.exists()) {
//file.delete();
continue;
} else {
try {
InputStream in = getResources().openRawResource(x);
FileOutputStream outStream = new FileOutputStream(file);
byte[] buff = new byte[1024];
byte[] buff1 = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
outStream.write(buff, 0, read);
//I wanted to fasten up the process so i added the code below: I have tried multiple way to do the same
//FileInputStream fi = new FileInputStream(myDir+File.separator+fname);
File File1 = new File(Environment.getExternalStorageDirectory()+File.separator +
"Application"+File.separator + "/Images" + "Image1-.jpg");
File File2 = new File(Environment.getExternalStorageDirectory()+File.separator + "Application"+File.separator + "/Images" + fname1);
//String name = in1.getName();
File newname = new File(fname1);
File1.renameTo(newname);
FileUtils.copyDirectory(File1, File2);
}
finally {
in.close();
outStream.close();
}
然而,这不起作用。 任何人都可以就如何解决这个问题给我一些建议吗?
答案 0 :(得分:0)
您可以通过此处读取目录中的图像 -
file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
调用copyBack()函数并传递文件路径和名称
String shiftedPath = copyBack(file.getAbsolutePath(),newName);
//函数用于复制和重命名
public String copyBack(String srcPath,String newName) {
//you can defined path of same directory as per your requirement, and just pass new
file = new File(Environment.getExternalStorageDirectory() + File.separator + "newName");
file.mkdirs();
try {
// File sd = Environment.getExternalStorageDirectory();
// File data = Environment.getDataDirectory();
String sourceImagePath = srcPath;
String destinationImagePath = file.getAbsolutePath();
File source = new File(sourceImagePath);
destination = new File(file, ".image");
if (source.exists()) {
FileChannel src = new FileInputStream(source).getChannel();
FileChannel dst = new FileOutputStream(destination).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return destination.getAbsolutePath();
}