0
我想製作一個具有上傳文件功能的應用程序。但問題是,我無法找到我做錯的地方。解析:無法上傳文件
首先,選擇文件
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
// intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
Log.d(TAG, "Select file");
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
RESULT_LOAD_FILE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(MainActivity.this, "Please install a File Manager.", Toast.LENGTH_SHORT).show();
}
// here
}
我想選擇基礎上的logcat文件時沒有任何問題。但是......
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
Log.d(TAG, requestCode+"/"+RESULT_LOAD_FILE+"/"+resultCode+"/"+RESULT_OK);
if (data != null) Log.d(TAG, data.toString());
else Log.d(TAG, "data null");
// get file name
String fileNameSegments[] = filePath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
// convert it to byte
byte[] fileByte = fileName.getBytes();
// Create the ParseFile
ParseFile file = new ParseFile(fileName, fileByte);
// Upload the image into Parse Cloud
file.saveInBackground();
// Create a New Class called "ImageUpload" in Parse
ParseObject fileupload = new ParseObject("FileUpload");
// Create a column named "ImageName" and set the string
fileupload.put("FileName", fileName);
// Create a column named "ImageFile" and insert the image
fileupload.put("DocFile", file);
// Create the class and the columns
fileupload.saveInBackground();
// Show a simple toast message
Toast.makeText(MainActivity.this, "File Uploaded", Toast.LENGTH_SHORT).show();
}
-1分別RESULT_LOAD_FILE
,resultCode
和RESULT_OK
1,1和-1的logcat秀requestCode
。並且數據不爲空,如在logcat中:Intent { dat=content://com.android.externalstorage.documents/document/0A09-1112:Download/Contact n Tort.pdf flg=0x1 }
當我單擊.pdf文件後,它觸發了敬酒Something went wrong
但我無法找到原因。
編輯: 擲空指針異常的文件路徑轉換後的字節,當我刪除try catch塊
提出異常的消息是什麼? –
@MoNazemi失敗將結果ResultInfo {who = null,request = 1,result = -1,data = Intent {dat = content:..........}}傳遞給activity {com.example .. ......... MainActivity}:java.lang.NullPointerException:嘗試調用空對象上的虛擬方法'java.lang.String [] java.lang.String.split(java.lang.String)'參考' – August
那麼它說變量'filePath'爲空,你在哪裏初始化? –