0

I'm rather new to Java (specifically Android). I'm trying to get the user to pick an image from the gallery, and then the app will copy the image from a gallery to a folder in the app's directory (as well as display the picture they have chosen in an imagebutton). However, I get a compiler error of "Unhandled exception type IOException."

This is my code: (Somewhere earlier)

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

(in onActivityResult function)

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

The error comes from the last line. Can anyone explain where I went wrong? I have no idea what the error means

Martijn Courteaux
  • 67,591
  • 47
  • 198
  • 287
chesnutcase
  • 500
  • 2
  • 7
  • 20

2 Answers2

2

You might want to use a

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

Also like mentioned you might want to look at Files

Java Devil
  • 10,629
  • 7
  • 33
  • 48
1

The error means you are doing an Input-Output operation and it is throwing an exception which is unhandled by you.

Use the try-catch blocks wherever there are IO operations normally.

Also instead of using that method, use the Files methods to copy or move the files, that will be easier.

You can refer to this link for File transfer details.

Vedang Jadhav
  • 510
  • 2
  • 11