【问题标题】:Java - "Unhandled exception type IOException"Java - “未处理的异常类型 IOException”
【发布时间】:2013-06-27 22:23:09
【问题描述】:

我对 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);

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

【问题讨论】:

  • 您是否有理由使用这种方法来复制文件而不是普通的 File 方法?我认为这不是一个很好的做法。
  • 不是真的,因为这是我知道的唯一XD方法

标签: java android ioexception


【解决方案1】:

你可能想用一个

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

也像提到的你可能想看看Files

【讨论】:

    【解决方案2】:

    该错误表示您正在执行输入-输出操作并且它抛出了一个您未处理的异常。

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

    也不要使用那个方法,而是使用 Files 方法来复制或移动文件,这样会更容易。

    文件传输详情可以参考this链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-01
      • 2023-03-12
      • 2018-06-03
      • 1970-01-01
      • 2016-07-04
      • 1970-01-01
      • 2011-01-19
      • 2017-12-02
      相关资源
      最近更新 更多