0

我对Java(特别是Android)相当陌生。我试图让用户从图库中选择图像,然后应用程序会将图像从图库复制到应用程序目录中的文件夹(以及显示他们在图像按钮中选择的图片)。但是,我收到“未处理的异常类型 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);

错误来自最后一行。谁能解释我哪里出错了?我不知道错误是什么意思

4

2 回答 2

2

您可能想使用

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

也像提到的那样,您可能想查看文件

于 2013-06-27T23:32:06.680 回答
1

该错误意味着您正在执行输入输出操作,并且它正在引发您未处理的异常。

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

另外,不要使用该方法,而是使用 Files 方法复制或移动文件,这样会更容易。

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

于 2013-06-28T07:29:27.747 回答