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

我无法加载所选图像。正在加载默认模板图像。我用帖子中的完整片段代码对其进行了编辑,您能指导我使用哪个参数上传到所选图像吗?


以下更新的代码有效,但仅上传预定义的图像,我无法上传选定的图像,我不知道该行使用哪个参数


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()

天涯尽头无女友
浏览 89回答 1
1回答

斯蒂芬大帝

这是一个空指针异常。imageUri 为空MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap如果您检查 getBitmap() 的实现。看public static final Bitmap getBitmap(ContentResolver cr, Uri url)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throws FileNotFoundException, IOException {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream input = cr.openInputStream(url);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Bitmap bitmap = BitmapFactory.decodeStream(input);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return bitmap;&nbsp; &nbsp; &nbsp; &nbsp; }你会发现它调用了 openInputStream() ,它需要一个非空的 uri。public final @Nullable InputStream openInputStream(@NonNull Uri uri)&nbsp; &nbsp;override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {&nbsp; &nbsp; &nbsp; &nbsp; super.onActivityResult(requestCode, resultCode, data)&nbsp; &nbsp; &nbsp; &nbsp; if (this.imageUri != null && requestCode == IMAGE_PICK_CODE && resultCode == RESULT_OK) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //Getting the Bitmap from Gallery&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, this.imageUri) as Bitmap&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.imgThumb!!.setImageBitmap(bitmap)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; this.pictureTaken = true&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (e:IOException) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(context, "Error loading image", Toast.LENGTH_LONG)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java