私はアンドロイド開発の初心者で、ImageButton に SDCard の画像を表示しようとしています。マニフェストに READ_EXTERNAL_STORAGE 権限があり、aFileChooserライブラリを使用して SD カード上のファイルを検索しています。imagebutton をクリックすると、目的の画像を選択できますが、画像ボタンに画像が表示されません。代わりに、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);
}