我是 android 的菜鸟,我想用来自 SD 卡的文件设置一个 ImageButton 图像。但是,getBitmap 并未创建工作位图。当我使用刚刚创建的位图设置 ImageButton 时,imageButton 的尺寸发生变化但图像没有出现。这真的很令人沮丧,非常感谢任何解决此问题的帮助。
我的代码
@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(Profile_Barber.this,"File Selected: "+file.getAbsolutePath(), Toast.LENGTH_LONG).show();
Log.e("URI", uri.toString());//Returns: content://media/external/images/media/1834
Bitmap bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
if(bmp==null){
Log.e("BMP NULL", "This that bullshit!");
}else{
Log.e("BMP NOT NULL", bmp.toString()); //Returns: BMP NOT NULL android.graphics.Bitmap@4152b5a0
profilepic.setImageBitmap(bmp);
}
} catch (Exception e) {
Log.e("FileSelectorTestActivity", "File select error", e);
e.printStackTrace();
}
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
最佳答案
用这个来解码图像怎么样?
Uri contentURI = Uri.parse(data.getDataString());
ContentResolver cr = getContentResolver();
InputStream in = cr.openInputStream(contentURI);
Bitmap pic = BitmapFactory.decodeStream(in,null,null);
关于android - 为什么 getBitmap 方法不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17751588/