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

3
  • stackoverflow.com/questions/6115896/… Commented Jun 27, 2013 at 22:25
  • Is there a reason why you are using this method to copy a file instead of the normal File methods? I don't think that is a very good practice. Commented Jun 27, 2013 at 22:29
  • Not really, because this is the only method I know of XD Commented Jun 27, 2013 at 22:41

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.