2013-06-27 86 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鏈接瞭解文件傳輸的詳細信息。

相關問題