我想在XMPP中發送帶有smack 4的文件,並且需要打開fileChooser
,但不幸的是我得到了標題中的錯誤。我看到很多像這樣的錯誤堆棧,但不幸的是他們不能幫助我。java.lang.RuntimeException:傳遞結果失敗ResultInfo {who = null,request = 0,result = -1,data = Intent {
這是我的文件選擇:
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:無法讀取文件
請幫我解決這個問題。謝謝。
謝謝。但你會介意更多關於'ContentResolver'和'openInputStream()'的解釋以及如何使用它們?請給我一點線索。十分感謝 – MHSFisher