Parse: cannot upload file

2019-09-08 09:13发布

I want to make an app which have upload file function. But the problem is, I unable to find where did I do wrong.

First, choose the file

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
        }

I guess there's no problem when choosing the file based on the logcat. But...

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

The logcat show requestCode, RESULT_LOAD_FILE, resultCode and RESULT_OK 1, 1, -1 and -1 respectively. And the data is not null, as in logcat: Intent { dat=content://com.android.externalstorage.documents/document/0A09-1112:Download/Contact n Tort.pdf flg=0x1 }

After I click the .pdf file, it triggered the toast Something went wrong but I can't find what the reason.


EDITED: Throw null pointer exception after convert the file path to byte, when I remove the try catch block

1条回答
我只想做你的唯一
2楼-- · 2019-09-08 09:48

it should be like this

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

though the class not created in parse dashboard but I guess that need another post.

查看更多
登录 后发表回答