我正在跟踪一个教程,该教程使用 uri 和 picasso 方法加载一些图像,直到您选择图像,然后选择应用程序崩溃。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.johan.portfolijs, PID: 13364
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:17998 flg=0x1 }} to activity {com.example.johan.portfolijs/com.example.johan.portfolijs.MainActivity}: java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to com.squareup.picasso.Target
at android.app.ActivityThread.deliverResults(ActivityThread.java:4605)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4647)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:49)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1948)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7045)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:964)
Caused by: java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to com.squareup.picasso.Target
at com.example.johan.portfolijs.MainActivity.onActivityResult(MainActivity.java:87)
at android.app.Activity.dispatchActivityResult(Activity.java:7759)
答案 0 :(得分:0)
在这里,在将其分配给URI时会遇到类强制转换异常,因此请再次检查代码。
答案 1 :(得分:0)
尝试一下,
data.getData().getPath()
或
Uri selectedImageURI = data.getData()
答案 2 :(得分:0)
添加此第一个条件应该可以:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_CANCELED) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}