【问题标题】:set image from device to ImageView not working将图像从设备设置为 ImageView 不起作用
【发布时间】:2021-02-03 21:45:44
【问题描述】:

我正在尝试将图像上传到云火存储,代码如下:

Uri userImagePath;
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageReference = storage.getReference("UserImages");

private void pickImage(View view) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && requestCode == RESULT_OK && data.getData()!=null){
            userImagePath = data.getData();
            userImage.setImageURI(userImagePath);
        }
    }

问题是在运行应用程序时,图像未设置为 imageview。 图像选择器已打开并允许选择图像,但它不显示在 imageview 中。 没有错误显示 但在 logcat 中显示如下:

2021-02-04 03:07:12.690 22126-23476/com.example.abcd D/FA: Application going to the background
2021-02-04 03:07:14.419 22126-22126/com.example.abcd D/InputTransport: Input channel constructed: fd=64
2021-02-04 03:07:19.339 22126-23476/com.example.abcd V/FA: Inactivity, disconnecting from the service

我也做了独立缓存/重新启动,我卸载了应用程序并重新安装。但问题仍然存在。所选图像未在 imageview 中显示。

【问题讨论】:

    标签: android android-studio android-intent imageview


    【解决方案1】:

    尝试从拾取的图像流中获取位图,如下所示:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && requestCode == RESULT_OK && data.getData()!=null){
            userImagePath = data.getData();
            
            try {
                InputStream inputStream = getContentResolver().openInputStream(userImagePath);  // to read image data
    
                // get a bitmap from a stream
                Bitmap image = BitmapFactory.decodeStream(inputStream);
                userImage.setImageBitmap(image);
    
    
            } catch (FileNotFoundException e) {
                Log.d("LOG_TAG", e.getMessage());
            }
    
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-27
      • 1970-01-01
      • 1970-01-01
      • 2013-06-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多