java.lang.RuntimeException:传递结果失败ResultInfo {who = null,request = 0,result = -1,data = Intent {

时间:2016-01-14 20:05:29

标签: java android android-intent xmpp smack

我想在XMPP中发送一个带有smack 4的文件,需要打开一个fileChooser但不幸的是我收到了标题中的错误。我在堆栈中看到很多这样的错误,但不幸的是,他们没有帮助我。

这是我的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}} to activity {finalproject.ffisher.com.finalproject / finalproject.ffisher.com.finalproject.ChatActivity}: java.lang.IllegalArgumentException:无法读取文件

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

1 个答案:

答案 0 :(得分:0)

您的getPath()实施对大多数Android设备和用户都是错误的。它仅适用于Uri的{​​{1}}值;你的MediaStore不是。

摆脱Uri。使用getPath()ContentResolver获取openInputStream()所代表内容的InputStream。然后使用Uri对象use sendStream()代替sendFile()