我是 android 开发的菜鸟,我正在尝试在 ImageButton 中显示来自 SDCard 的图像。我的清单中有 READ_EXTERNAL_STORAGE 权限,我正在使用aFileChooser库来查找我的 SD 卡上的文件。当我单击我的图像按钮时,它允许我选择我想要的图像,但不会在图像按钮中显示图像。相反,ImageButton 完全消失了。我没有收到错误,但 ImageButton 变为空白。任何帮助是极大的赞赏。
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);
}