Java 解析:无法上载文件
我想做一个有上传文件功能的应用。但问题是,我找不到哪里做错了 首先,选择文件Java 解析:无法上载文件,java,android,file-upload,parse-platform,Java,Android,File Upload,Parse Platform,我想做一个有上传文件功能的应用。但问题是,我找不到哪里做错了 首先,选择文件 public void onClick(View arg0) { Intent intent = new Intent(Intent.ACTION_GET_CONTENT); intent.setType("*/*"); // intent.addCategory(Intent.CATEGORY_OPENABLE); t
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();
}
日志猫分别显示requestCode
、RESULT\u LOAD\u FILE
、resultCode
和RESULT\u OK
1、1、-1和-1。并且数据不为null,如logcat:Intent{dat中所示=content://com.android.externalstorage.documents/document/0A09-1112:Download/Contact n.pdf flg=0x1}
在我点击.pdf文件后,它触发了toast出现了一些问题,但我找不到原因
编辑:
将文件路径转换为字节后抛出空指针异常,当我删除try-catch块时,应该是这样的
Uri uri = data.getData();
// get path
filePath = uri.getPath();
// 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 file into Parse Cloud
file.saveInBackground();
// Create a New Class called "FileUpload" in Parse
ParseObject fileUpload = new ParseObject("FileUpload");
// Create a column named "FileName" and set the string
fileUpload.put("FileName", fileName);
Log.d(TAG, "image file");
// Create a column named "ImageFile" and insert the image
fileUpload.put("DocFile", file);
// Create the class and the columns
fileUpload.saveInBackground();
Log.d(TAG, "toast");
// Show a simple toast message
Toast.makeText(MainActivity.this, "File Uploaded",
Toast.LENGTH_SHORT).show();
虽然类不是在parse dashboard中创建的,但我想这需要另一篇文章。引发异常的消息是什么???@MoNazemi未能将结果ResultInfo{who=null,request=1,result=-1,data=Intent{dat=content:..}传递到活动{com.example…….MainActivity}:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“java.lang.String[]java.lang.String.split(java.lang.String)”
它说变量filePath
为空,在哪里初始化??@MoNazemiString selectedFile=data.getData().getPath()代码>啊!?为什么要这样做stringfilenamesegments[]=filePath.split(“/”)代码>然后呢?