为了账号安全,请及时绑定邮箱和手机立即绑定

从图库中选择的图像出现 Kotlin 错误

从图库中选择的图像出现 Kotlin 错误

天涯尽头无女友 2022-11-02 16:02:23
我无法加载所选图像。正在加载默认模板图像。我用帖子中的完整片段代码对其进行了编辑,您能指导我使用哪个参数上传到所选图像吗?以下更新的代码有效,但仅上传预定义的图像,我无法上传选定的图像,我不知道该行使用哪个参数if (this.imageUri != imageUri && requestCode == IMAGE_PICK_CODE && resultCode == RESULT_OK) {class  ReportFragment : Fragment(), OnMapReadyCallback, GoogleMap.OnMapClickListener, GoogleMap.OnCameraIdleListener {    private var mMapView: MapView? = null    private var map: GoogleMap? = null    val CAMERA_PERMISSION_CODE = 0    val CAMERA_REQUEST_CODE = 10    lateinit var imageFilePath: String    private var imgThumb: ImageView? = null    private var spinner: Spinner? = null    private var currentLocation: LatLng? = null  // TODO: get current location as static variable    private var midLatLng: LatLng? = null    private var lat: Double? = null    private var lng: Double? = null    private var objectValues: Array<String>? = null    private var imageUri: Uri? = null    private var pictureTaken: Boolean = false    private var progress: ProgressBar? = null    val PERMISSION_CODE_READ = 1001    val PERMISSION_CODE_WRITE = 1002    val IMAGE_PICK_CODE = 1000    private val database: FirebaseDatabase? = FirebaseDatabase.getInstance()    private val markersRef: DatabaseReference? = database!!.getReference("markers")    private val storage = FirebaseStorage.getInstance()    private val storageRef: StorageReference = storage.reference    private var imagesRef: StorageReference? = null    private val permissoes = arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE)    inner class GenericFileProvider : FileProvider()
查看完整描述

1 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

这是一个空指针异常。imageUri 为空


MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap

如果您检查 getBitmap() 的实现。



public static final Bitmap getBitmap(ContentResolver cr, Uri url)

                throws FileNotFoundException, IOException {

            InputStream input = cr.openInputStream(url);

            Bitmap bitmap = BitmapFactory.decodeStream(input);

            input.close();

            return bitmap;

        }

你会发现它调用了 openInputStream() ,它需要一个非空的 uri。


public final @Nullable InputStream openInputStream(@NonNull Uri uri)



   override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {

        super.onActivityResult(requestCode, resultCode, data)

        if (this.imageUri != null && requestCode == IMAGE_PICK_CODE && resultCode == RESULT_OK) {


            try {

                //Getting the Bitmap from Gallery

                val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap

                this.imgThumb!!.setImageBitmap(bitmap)

                this.pictureTaken = true

            } catch (e:IOException) {

                e.printStackTrace()

            }

        } else {

            Toast.makeText(context, "Error loading image", Toast.LENGTH_LONG)

        }

    }


查看完整回答
反对 回复 2022-11-02
  • 1 回答
  • 0 关注
  • 85 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信