2

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.

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
    }
}

this is my 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);
}

and this is my getPath method :

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);

and finally this error in reported :

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.

MHSaffari
  • 858
  • 1
  • 16
  • 39

1 Answers1

0

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.

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 2
    thank you. but would you mind more explain about `ContentResolver` and `openInputStream()` and how to use them ? please give me a clue. thank u very much – MHSaffari Jan 14 '16 at 20:27