如何将图像按钮设置为我内部存储中的某个图像 (Android)?

How to set Image Button to some image in my internal storage (Android)?

帮我把图片按钮的uri设置成我select从内部存储的图片。我已经在 android_manifest.xml 文件中提供了所需的权限。问题是当我浏览到 phone 的内部存储时,图像无法select。

public void onUploadImageClick(View view){
    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
    galleryIntent.setType("Image/*");
    startActivityForResult(galleryIntent, GALLERY_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
        uri=data.getData();
        imageButton = (ImageButton)findViewById(R.id.image_button);
        imageButton.setImageURI(uri);
    }
}

你能试试这个吗?您可以将 ImageView 替换为 ImageButton

 String qrPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Image.png";
 BitmapFactory.Options options = new BitmapFactory.Options();
 options.inPreferredConfig = Bitmap.Config.ARGB_8888;
 Bitmap mustOpen = BitmapFactory.decodeFile(qrPath, options);

 setContentView(R.layout.qr_image);
 ImageView imageView = (ImageView) findViewById(R.id.qr_image);
 imageView.setImageBitmap(mustOpen);

您可以试试下面的代码

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
        uri=data.getData();
        String pathName = uri.toString();
        Bitmap bmp = BitmapFactory.decodeFile(pathName);
        imageButton = (ImageButton)findViewById(R.id.image_button);
        imageButton.setImageBitmap(bmp);
    }
}
public void onUploadImageClick(View view){

Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.EXTERNAL_CONTENT_URI);

startActivityForResult(galleryIntent, GALLERY_REQUEST);

}

然后在你的activity结果方法中保留这个

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent    data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == GALLERY_REQUEST && resultCode == RESULT_OK){
    uri=data.getData();
    imageButton = (ImageButton)findViewById(R.id.image_button);
    imageButton.setImageURI(uri);
}

}

编辑

是的,问题是我忘了添加 .Media 所以使用这行代码:

 Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);