我有一个方法:copyImage
多数民众赞成在ImageLoder
类内接受两个字符串参数( source image, target image),如下所示:
public Class ImageLoader{
public boolean copyIMAGE(String source, String target){
File sourceFile=new File(Environment.getExternalStorageDirectory()+source);
File targetFile=new File(Environment.getExternalStorageDirectory()+target);
InputStream fis;
OutputStream fos;
BufferedOutputStream bufferIS;
try{
fis=new FileInputStream(destinationFile);
fos=new FileOutputStream(targetFile);
bufferIS=new BufferedOutputStream(fos);
byte[] b=new byte[1024];
int len=0;
try
{
while ((len = fis.read(b)) != -1)
{
fos.write(len);
}
bufferIS.close();
fos.close();
fis.close();
return true;
}
catch (IOException e)
{
}
}catch(FileNotFoundException e){
}
return false;
}
}
MainActivity类中的copyButton被单击后触发的方法,并且copyImage返回false
copyButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view)
{
// TODO: Implement this method
String source="/storage/emulated/0/Download/my image.jpeg";
String target ="/storage/emulated/0/Download//images/my image.jpeg";
loader.copyIMAGE(source,target);
}
});
该方法返回false,并且出现FileNotFound异常。
Ps :源文件存在, 目的地存在, 我具有 rw 权限。
答案 0 :(得分:0)
您为什么使用Environment.getExternalStorageDirectory()
?将其删除,因为source
和target
已经是完整路径。
File destinationFile=new File(source);
File targetFile=new File(target);
您的复制功能也不正确,您正在使用错误的参数调用write
。
while ((len = fis.read(b)) != -1)
{
fos.write(b, 0, len);
}