【问题标题】:java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent {java.lang.RuntimeException: 传递结果失败 ResultInfo{who=null, request=0, result=-1, data=Intent {
【发布时间】:2016-01-14 20:05:28
【问题描述】:

我想在 XMPP 中发送一个带有 smack 4 的文件,并且需要打开一个 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: 无法读取文件

请帮我解决这个问题。谢谢。

【问题讨论】:

    标签: java android android-intent xmpp smack


    【解决方案1】:

    对于大多数 Android 设备和用户而言,您的 getPath() 实现是错误的。它仅适用于来自MediaStoreUri 值;你的Uri 不是。

    摆脱getPath()。使用ContentResolveropenInputStream()Uri 所代表的内容上获得InputStream。然后 use sendStream() 而不是 sendFile() 与您的 OutgoingFileTransfer 对象。

    【讨论】:

    • 谢谢。但是您介意更多解释ContentResolveropenInputStream() 以及如何使用它们吗?请给我一个线索。非常感谢你
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多