代码之家  ›  专栏  ›  技术社区  ›  user7633957

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

  •  1
  • user7633957  · 技术社区  · 7 年前

    帮助我将图像按钮的uri设置为我从内部存储器中选择的图像。我已经在android_清单中授予了所需的权限。xml文件。问题是,当我浏览手机的内部存储时,图像是无法选择的。

    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);
        }
    }
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   Hasan Bou Taam    7 年前
    public void onUploadImageClick(View view){
    
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.EXTERNAL_CONTENT_URI);
    
    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);
    }
    

    }

    编辑

    是的,问题是我忘了添加。媒体 所以使用这行代码:

     Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    
        2
  •  0
  •   Flutterian    7 年前

    您可以尝试以下代码

    @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);
        }
    }
    
        3
  •  0
  •   delavega66 JDGuide    7 年前

    你能试试这个吗。你可以替换 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);