亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

從圖庫中選擇的圖像出現 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 關注
  • 122 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號