图片上传不仅适用于Mi手机

时间:2017-11-08 08:15:05

标签: java android image onactivityresult

这是我在尝试从Mi mobile上传图片时遇到的错误。但对于其他手机,它工作正常

java.lang.RuntimeException: Failure delivering result 
ResultInfo{who=null, request=1, result=-1, data=Intent { flg=0x1 clip={image/* text/uri-list U:content://com.miui.gallery.open/raw/%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20171107_230239.jpg} (has extras) }} to activity {com.fullstacknet.chatapp/com.fullstacknet.chatapp.ChatActivity}: 
java.lang.IllegalArgumentException: Invalid URI: content://com.miui.gallery.open/raw/%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20171107_230239.jpg
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4108)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4151)
    at android.app.ActivityThread.-wrap20(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1551)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6195)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764)
Caused by: java.lang.IllegalArgumentException: Invalid URI: content://com.miui.gallery.open/raw/%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20171107_230239.jpg
    at android.provider.DocumentsContract.getDocumentId(DocumentsContract.java:917)
    at com.fullstacknet.chatapp.ChatActivity.getRealPathFromURI(ChatActivity.java:896)
    at com.fullstacknet.chatapp.ChatActivity.onActivityResult(ChatActivity.java:962)
    at android.app.Activity.dispatchActivityResult(Activity.java:6998)
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4104)
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4151) 
    at android.app.ActivityThread.-wrap20(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1551) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6195) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:874) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:764) 
    11-08 13:38:59.261 28831-28831/com.fullstacknet.chatapp E/MQSEventManagerDelegate: failed to get MQSService.

我使用此Intent选择图像:

 Intent intent = new Intent();
 intent.setType("image/*");
 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
 intent.setAction(Intent.ACTION_GET_CONTENT);
 startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);

选择图像后没有点击onActivityResult

请帮我解决这个问题。

由于

2 个答案:

答案 0 :(得分:1)

如果您在日志中看到URI

URI: content://com.miui.gallery.open/raw/%2Fstorage%2Femulated%2F0%2FDCIM%2FCamera%2FIMG_20171107_230239.jpg

此处%2F未正确解码

它应该是这样的:

content://com.miui.gallery.open/raw//storage/emulated/0/DCIM/Camera/IMG_20171107_230239.jpg

但我仍然认为它不会起作用,因为它是定制的。

最好返回/storage/emulated/0/DCIM/Camera/IMG_20171107_230239.jpg 作为实际路径,然后根据需要获取URI。

答案 1 :(得分:0)

完美的解决方案是 在清单文件中添加以下实体

android:hardwareAccelerated="false"

android:largeHeap="true"