如何在Android上将图像字符串转换为整数?
Posted
技术标签:
【中文标题】如何在Android上将图像字符串转换为整数?【英文标题】:How to convert an image string into integer on Android? 【发布时间】:2021-09-17 06:21:31 【问题描述】:我想从 Firebase 检索图像并将其放置在画布中,以便使用它放置在自定义 google 标记中。使用 .toInt() 方法时出现错误。也许这是一种错误的做法。
代码:
var loggedInUser : User = User()
mFireStore.collection(Constants.USERS)
.document(FirestoreClass().getCurrentUserId())
.get()
.addOnSuccessListener document ->
loggedInUser = document.toObject(User::class.java)!!
canvas1.drawBitmap(BitmapFactory.decodeResource(resources,loggedInUser.image.toInt()), 0F, 0F, color)
canvas1.drawText("User Name!", 30F, 40F, color)
// add marker to Map
// add marker to Map
markerOptions
.position(LatLng(mLastLocation!!.latitude, mLastLocation!!.longitude))
.title("Current Location")
/*.snippet(address)*/
.icon(BitmapDescriptorFactory.fromBitmap(bmp))
mCurrentMarker = googleMap.addMarker(markerOptions)
我得到错误的 logcat:
2021-07-06 19:33:54.839 10676-10676/com.example.aroundyouapp E/androidRuntime: FATAL EXCEPTION: main
Process: com.example.aroundyouapp, PID: 10676
java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:627)
at java.lang.Integer.parseInt(Integer.java:650)
at com.example.aroundyouapp.activities.MainActivity$getCurrentLocation$1.onLocationResult(MainActivity.kt:394)
at com.google.android.gms.internal.location.zzau.notifyListener(Unknown Source:4)
at com.google.android.gms.common.api.internal.ListenerHolder.notifyListenerInternal(com.google.android.gms:play-services-base@@17.1.0:17)
at com.google.android.gms.common.api.internal.ListenerHolder$zaa.handleMessage(com.google.android.gms:play-services-base@@17.1.0:6)
at android.os.Handler.dispatchMessage(Handler.java:107)
at com.google.android.gms.internal.base.zar.dispatchMessage(com.google.android.gms:play-services-base@@17.1.0:8)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
如何将字符串格式的图像(即 loggedInUser.image)转换为 Int,以便将图像放置在画布上?也许这是一个愚蠢的问题,但我是新手,所以是的。
提前谢谢你
编辑 1:
我用来在导航菜单中显示用户头像的代码:
fun loadUserData(activity: Activity)
//This function is called in MainActivity in the onCreate method.
mFireStore.collection(Constants.USERS)
.document(getCurrentUserId())
.get()
.addOnSuccessListener document ->
val loggedInUser = document.toObject(User::class.java)
when(activity)
is MyProfileActivity ->
activity.setUserDataInUI(loggedInUser!!)
is MainActivity ->
activity.updateNavigationUserDetails(loggedInUser!!)
fun updateNavigationUserDetails(user: User)
hideProgressDialog()
mUserName = user.name
Glide
.with(this)
.load(user.image)
.centerCrop()
.placeholder(R.drawable.ic_user_place_holder)
.into(nav_user_image)
tv_username.text = user.name
编辑 2:
我尝试了 Ragu 的回答。该应用在尝试后崩溃了。
这是 logcat 中的错误。
2021-07-14 19:07:04.779 18663-18663/com.example.aroundyouapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aroundyouapp, PID: 18663
java.lang.NumberFormatException: For input string: "https://firebasestorage.googleapis.com/v0/b/aroundyou-5f1d2.appspot.com/o/USER_IMAGE1624282533208.jpg?alt=media&token=731ea81d-32a0-4378-aa7b-0d7764260e41"
at java.lang.Integer.parseInt(Integer.java:615)
at java.lang.Integer.parseInt(Integer.java:650)
at com.example.aroundyouapp.activities.MainActivity$getCurrentLocation$1$onLocationResult$1.onSuccess(MainActivity.kt:374)
at com.example.aroundyouapp.activities.MainActivity$getCurrentLocation$1$onLocationResult$1.onSuccess(MainActivity.kt:349)
at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
我上次也遇到了同样的错误,但这次图片的链接显示在 logcat 中
编辑 3
尝试 Ragu 更新的答案 2 后出现此错误
2021-07-14 20:05:57.399 20234-20234/com.example.aroundyouapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aroundyouapp, PID: 20234
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1565)
at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:115)
at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:103)
at java.net.InetAddress.getAllByName(InetAddress.java:1152)
at com.android.okhttp.Dns$1.lookup(Dns.java:41)
at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:178)
at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:144)
at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:86)
at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:176)
at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:128)
at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:97)
at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:289)
at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:232)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:465)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:411)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:248)
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:211)
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:30)
at com.example.aroundyouapp.activities.MainActivity$getCurrentLocation$1$onLocationResult$1.onSuccess(MainActivity.kt:379)
at com.example.aroundyouapp.activities.MainActivity$getCurrentLocation$1$onLocationResult$1.onSuccess(MainActivity.kt:352)
at com.google.android.gms.tasks.zzn.run(Unknown Source:4)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
【问题讨论】:
请从loggedInUser.image.toInt()中删除.toInt() 如果我不添加它,那么它会显示“必需:Int,找到:String”。你能帮我解决这个问题吗? 在这种情况下只检查空字符串.. if (loggedInUser.image.toString(),length() > 0) canvas1.drawBitmap(BitmapFactory.decodeResource(resources,loggedInUser.image.toInt ()), 0F, 0F, 颜色) 现在没有标记显示了lol 我认为字符串格式的图像是某种方式?也许我们不能把它转换成 Int 并让它工作我猜? 【参考方案1】:假设您正在尝试显示Base64
格式的图像。
将传入的Base64
字符串解码为ByteArray
val imageByteArray = Base64.decode(user.image, Base64.DEFAULT)
然后将imageByteArray
加载到Glide
,如下所示。
Glide
.with(this)
.asBitmap()
.load(imageByteArray) // ==> Pass the ByteArray here
.into(nav_user_image)
编辑 1:更新答案
从您的代码中我可以看到,绘制到画布代码位于snapshots
之外的addOnSuccessListener
之外。但是在分配数据的addOnSuccessListener
完成之前,您正在访问loggedInUser
的字段。
addOnSuccessListener
是async
操作,不会被执行 按照您在现有代码中编写的顺序。
将以下代码移入addOnSuccessListener
。
canvas1.drawBitmap(BitmapFactory.decodeResource(resources,loggedInUser.image.toInt()), 0F, 0F, color)
canvas1.drawText("User Name!", 30F, 40F, color)
您现有的代码。
mFireStore.collection(Constants.USERS)
.document(FirestoreClass().getCurrentUserId())
.get()
.addOnSuccessListener document ->
loggedInUser = document.toObject(User::class.java)!!
canvas1.drawBitmap(BitmapFactory.decodeResource(resources,loggedInUser.image.toInt()), 0F, 0F, color)
canvas1.drawText("User Name!", 30F, 40F, color)
更新答案 2:
根据错误日志,将图像作为远程 url 检索,可以直接在ImageView
中加载。但是当您尝试在canvas
中绘制它时,您需要将远程url 中的文件解码为stream
,然后将其设置在Canvas
中。
类似下面的东西
URL url = new URL(<PASS YOUR URL from FIREBASE>);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
....
Bitmap receivedImageasBitmap = BitmapFactory.decodeStream(connection .getInputStream());
..
canvas1.drawBitmap(receivedImageasBitmap)
【讨论】:
我试过了。我没有使用 Glide,因为我想在画布上绘制该图像,所以当我尝试您的解决方案时,它说在这行代码中找到“ByteArray”必需“Int”。canvas1.drawBitmap(BitmapFactory.decodeResource(resources,imageByteArray), 0F, 0F, color)
检查我更新的答案,让知道它是否有效! @perpetualdarkness
嘿,我试过你的代码,应用程序崩溃了。我用 logcat 中的错误更新了我的问题。
检查我的更新答案 2 @perpetualdarkness
试过了。又报错了。请检查更新的问题以上是关于如何在Android上将图像字符串转换为整数?的主要内容,如果未能解决你的问题,请参考以下文章