2012-02-15 87 views
0

我正在尝试使用此代码。文件未找到复制Android文件时出现异常

public synchronized void onActivityResult(final int requestCode, int resultCode, 
     final Intent data) { 
    String filePath = null; 
    if (resultCode == Activity.RESULT_OK) { 
     switch (requestCode) { 
     case RESULT_MEMORY_SAVE:    
      filePath = data.getDataString(); 
      copyFileForReceipts(filePath,pathOfReceipts); 

现在filepath是一个字符串,调试器显示出其价值"content://media/external/images/media/133"

接下来我将尝试使用此:

sourceLocation = filePath; 
File afile = new File(sourceLocation); 
File bfile = new File(targetLocation + "file.jpg"); 
inStream = new FileInputStream(afile.getName()); //Gives exception 
outStream = new FileOutputStream(bfile.getName()); 

它给人FileNotFound例外的例外。我也尝试删除.getName()

请告诉我我在哪里错了。

问候

回答

1

而是采用使用setDatagetDataString,你应该把文件路径和检索如下:

添加的结果路径生产活动:

intent.putString("path", file_path); 

并检索路径:

filePath = data.getString("path"); 
copyFileForReceipts(filePath,pathOfReceipts); 

并以其良好的做法,就可以进一步做任何事情之前检查文件是否存在:

if(afile.exists()){ 
    //do whatever you want 
}