2013-06-27 85 views
0

我对Java(特别是Android)颇为陌生。我试图让用户从图库中选择图片,然后应用程序会将图片从图库复制到应用程序目录中的文件夹(以及显示他们在图像按钮中选择的图片)。但是,我收到“未处理的异常类型IOException”的编译器错误。Java - “未处理的异常类型IOException”

这是我的代码: (某处更早)

Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(i, 2); 

(在onActivityResult功能)

Uri selectedImage = data.getData(); //data from onActivityResult 
String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); 
cursor.moveToFirst(); 
int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
String picturePath = cursor.getString(columnIndex); 
cursor.close(); 
ImageButton abcd = (ImageButton) findViewById(R.id.btn_addpicture); 
abcd.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
String command = "cp " + "'" + selectedImage.getPath().toString() + "' '" + getFilesDir().getPath() + "/Images/" + VALUE_NAME + ".jpg"; 
Runtime.getRuntime().exec(command); 

这个错误来自最后一行。任何人都可以解释我出错的地方吗?我不知道是什么错误意味着

+0

http://stackoverflow.com/questions/6115896/java-checked-vs-unchecked-exception-explanation –

+0

是否有一个原因,你使用这种方法来复制一个文件,而不是正常的文件方法?我不认为这是一个很好的做法。 – SimonSays

+0

不是真的,因为这是我所知道的XD唯一的方法 – chesnutcase

回答

1

你可能想使用一个

try{ 
    //your code 
} catch(IOException ioEx) { 
    ioEx.printStackTrace() // or what ever you want to do with it 
} 

还喜欢提到的,你可能想看看Files

0

的错误意味着你正在做的输入输出操作它抛出一个你未处理的异常。

正常情况下有IO操作的地方使用try-catch块。

而不是使用该方法,使用Files方法来复制或移动文件,这将更容易。

您可以参考this链接了解文件传输的详细信息。

相关问题