Android 更改ImageButton的图像

Android 更改ImageButton的图像,android,imagebutton,Android,Imagebutton,我有一个ImageButton,它最初设置为可绘制资源。在活动期间,我想将图像设置为用户照片的位图。拍照和保存操作正常,但ImageButton的图像不变 这是我的密码: Bitmap bitmap = BitmapFactory.decodeFile(PATH + "/image.jpg"); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JP

我有一个ImageButton,它最初设置为可绘制资源。在活动期间,我想将图像设置为用户照片的位图。拍照和保存操作正常,但ImageButton的图像不变

这是我的密码:

Bitmap bitmap = BitmapFactory.decodeFile(PATH + "/image.jpg");

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, stream);

Log.d(TAG, bitmap.toString());  //prints out android.graphics.Bitmap@41d772a8
rearEndImageButton.setImageBitmap(bitmap);

要从sd卡上完成此操作,您可以尝试使用

Bitmap bmp = BitmapFactory.decodeFile("path_to_file");
ImageButton rearEndImageButton = (ImageButton)findViewById(R.id.rearEndImageButton);
rearEndImageButton.setImageBitmap(bmp);
在activityonCreate方法中,将您的按钮分配给一个变量,然后设置一个资源或位图,该资源或位图将使用BitmapFactory类从文件中解码。

您可以使用文件资源管理器从sd卡中选择图像,并将该图像路径分配给
BitmapFactory.decodeFile()。

为什么需要压缩。您可以设置位图directly@Raghunandan我需要高质量的图像。不管怎样,删除那行代码并没有改变任何事情。你列出的代码和我在问题中的代码是一样的。我不想从列表中选择,我已经知道图像的路径。然后使用正确的图像路径,确认您的文件存在。请参阅类似的内容适用于textView和imageView,,