如何在 Android 上将图像字符串转换为整数?
How to convert an image string into integer on Android?
我想从 Firebase 检索图像并将其放置在 canvas 中,以便我使用它放置在自定义 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.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,以便将图像放置在 canvas ?也许这是一个愚蠢的问题,但我是新手所以是的。
提前致谢
编辑 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$onLocationResult.onSuccess(MainActivity.kt:374)
at com.example.aroundyouapp.activities.MainActivity$getCurrentLocation$onLocationResult.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)
我上次遇到了同样的错误,但这次 link 图像显示在 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.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$onLocationResult.onSuccess(MainActivity.kt:379)
at com.example.aroundyouapp.activities.MainActivity$getCurrentLocation$onLocationResult.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)
假设您正在尝试显示 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:更新答案
从您的代码中我可以看出,绘制到 canvas 的代码位于 snapshots
的 addOnSuccessListener
之外。但是您在分配数据的 addOnSuccessListener
完成之前访问 loggedInUser
的字段。
addOnSuccessListener
is async
operation and will not be executed
in sequence like you written in your existing code.
将下面的代码移到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)
我想从 Firebase 检索图像并将其放置在 canvas 中,以便我使用它放置在自定义 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.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,以便将图像放置在 canvas ?也许这是一个愚蠢的问题,但我是新手所以是的。
提前致谢
编辑 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$onLocationResult.onSuccess(MainActivity.kt:374)
at com.example.aroundyouapp.activities.MainActivity$getCurrentLocation$onLocationResult.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)
我上次遇到了同样的错误,但这次 link 图像显示在 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.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$onLocationResult.onSuccess(MainActivity.kt:379)
at com.example.aroundyouapp.activities.MainActivity$getCurrentLocation$onLocationResult.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)
假设您正在尝试显示 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:更新答案
从您的代码中我可以看出,绘制到 canvas 的代码位于 snapshots
的 addOnSuccessListener
之外。但是您在分配数据的 addOnSuccessListener
完成之前访问 loggedInUser
的字段。
addOnSuccessListener
isasync
operation and will not be executed in sequence like you written in your existing code.
将下面的代码移到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)