我是android開發的noob,我試圖在ImageButton中顯示來自SDCard的圖像。我的清單中有READ_EXTERNAL_STORAGE權限,我正在使用aFileChooser庫來查找我的SD卡上的文件。當我點擊我的圖像按鈕時,它允許我選擇我想要的圖像,但不顯示圖像按鈕中的圖像。 ImageButton完全消失。我沒有收到錯誤,但ImageButton變爲空白。任何幫助是極大的讚賞。爲什麼不從SDCard顯示圖像?
private void showChooser() {
// Use the GET_CONTENT intent from the utility class
Intent target = FileUtils.createGetContentIntent();
// Create the chooser Intent
Intent intent = Intent.createChooser(
target, getString(R.string.chooser_title));
try {
startActivityForResult(intent, REQUEST_CODE);
} catch (ActivityNotFoundException e) {
// The reason for the existence of aFileChooser
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE:
// If the file selection was successful
if (resultCode == RESULT_OK) {
if (data != null) {
// Get the URI of the selected file
final Uri uri = data.getData();
try {
// Create a file instance from the URI
final File file = FileUtils.getFile(uri);
Toast.makeText(RegisterActivity.this,"File Selected: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
Log.e("File Path", file.getAbsolutePath());// Returns /external/images/media/1830
Log.e("BMP NULL", bmp.toString()); //Throws NullPointerException
Bitmap bmp = BitmapFactory.decodeFile(file.getAbsolutePath());//Seems to work fine here
userpic.setImageBitmap(bmp); //ImageButton disappears/goes blank here.
} catch (Exception e) {
Log.e("FileSelectorTestActivity", "File select error", e);
}
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
代碼userpic初始化,並使用findviewbyid分配給一個看法? – ChrisBorg
是的。我在onCreate期間這樣做。 – DollaBill