java.lang.RuntimeException:无法交付结果ResultInfo {who = null,request = 0,result = -1,data = Intent { [英] java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent {

查看:1028
本文介绍了java.lang.RuntimeException:无法交付结果ResultInfo {who = null,request = 0,result = -1,data = Intent {的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在XMPP中发送带有smack 4的文件,并且需要打开fileChooser,但是不幸的是我收到了标题中出现的错误.我在堆栈中看到了很多类似这样的错误,但是不幸的是没有一个无法帮助我.

I want to send a file with smack 4 in XMPP and need to open a fileChooser but unfortunately i got the error that put in title. i see many errors like this in stack but unfortunately non of them couldn't help me.

这是我的fileChooser:

this is my fileChooser :

private void showFileChooser() {
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    try {
        startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"),FILE_SELECT_CODE);
    } catch (android.content.ActivityNotFoundException ex) {
        // Potentially direct the user to the Market with a Dialog
    }
}

这是我的onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    File source = null;
    String filename = null;
    switch (requestCode) {
        case FILE_SELECT_CODE:
            if (resultCode == RESULT_OK) {
                final Uri uri = data.getData();
                try {
                    String src = getPath(ChatActivity.this, uri);
                    source = new File(Environment.getExternalStorageDirectory() + "/" + src);
                    filename = uri.getLastPathSegment();
                } catch (URISyntaxException e) {
                    e.printStackTrace();
                }
   //================this is the Smack part of code :
                final FileTransferManager manager = FileTransferManager.getInstanceFor(connection);

// Create the outgoing file transfer
                OutgoingFileTransfer transfer = manager.createOutgoingFileTransfer(user + "/" + roster.getPresenceResource(user));
                // Send the file
                try {
                    transfer.sendFile(source,"Hi");
                } catch (SmackException e) {
                    e.printStackTrace();
                }
//============Smack part finished
            }
            break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

这是我的getPath方法:

public static String getPath(Context context, Uri uri) throws URISyntaxException {

    String path = null;
    String[] projection = { MediaStore.Files.FileColumns.DATA };
    @SuppressWarnings("deprecation")
    Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

    if(cursor == null){
        path = uri.getPath();
    }
    else{
        cursor.moveToFirst();
        int column_index = cursor.getColumnIndexOrThrow(projection[0]);
        path = cursor.getString(column_index);
        cursor.close();
    }

    return ((path == null || path.isEmpty()) ? (uri.getPath()) : path);

最后报告了此错误:

01-14 23:24:09.099 12580-12580/finalproject.ffisher.com.finalproject E/AndroidRuntime:java.lang.RuntimeException:无法传送结果ResultInfo {who = null,request = 0,result = -1, data = Intent {dat = content://com.android.providers.downloads.documents/document/125 flg = 0x1}}以进行活动{finalproject.ffisher.com.finalproject/finalproject.ffisher.com.finalproject.ChatActivity}: java.lang.IllegalArgumentException:无法读取文件

01-14 23:24:09.099 12580-12580/finalproject.ffisher.com.finalproject E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/125 flg=0x1 }} to activity {finalproject.ffisher.com.finalproject/finalproject.ffisher.com.finalproject.ChatActivity}: java.lang.IllegalArgumentException: Could not read file

请帮助我解决这个问题.谢谢.

please help me to solve this problem. thank you.

推荐答案

对于大多数Android设备和用户,您的getPath()实现是错误的.它仅适用于MediaStore中的Uri值;您的Uri不是.

Your getPath() implementation is wrong for most Android devices and users. It will only work for Uri values from the MediaStore; your Uri is not.

摆脱getPath().使用ContentResolveropenInputStream()Uri表示的内容上获取InputStream.然后

Get rid of getPath(). Use ContentResolver and openInputStream() to get an InputStream on the content represented by the Uri. Then use sendStream() instead of sendFile() with your OutgoingFileTransfer object.

这篇关于java.lang.RuntimeException:无法交付结果ResultInfo {who = null,request = 0,result = -1,data = Intent {的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆