解析:无法上传文件

时间:2015-10-19 17:04:18

标签: 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);

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

logcat分别显示requestCodeRESULT_LOAD_FILEresultCodeRESULT_OK 1,1,-1和-1。并且数据不为null,如logcat:Intent { dat=content://com.android.externalstorage.documents/document/0A09-1112:Download/Contact n Tort.pdf flg=0x1 }

点击.pdf文件后,它触发了toast Something went wrong,但我找不到原因。

EDITED: 将文件路径转换为byte后,当我删除try catch块

时,抛出空指针异常

1 个答案:

答案 0 :(得分:0)

应该是这样的

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

虽然这个类没有在解析仪表板中创建,但我想还需要另一个帖子。