解析:无法上传文件 [英] Parse: cannot upload file

查看:274
本文介绍了解析:无法上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想制作一个具有上传文件功能的应用程序。但问题是,我无法找到我在哪里做错了。



首先,选择文件

 public void onClick(View arg0){

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType(* / *);
// intent.addCategory(Intent.CATEGORY_OPENABLE);

尝试{
Log.d(TAG,Select file);
startActivityForResult(
Intent.createChooser(intent,选择要上传的文件),
RESULT_LOAD_FILE);
} catch(android.content.ActivityNotFoundException ex){
//可能用一个对话框将用户带到市场
Toast.makeText(MainActivity.this,请安装一个文件管理器。 ,Toast.LENGTH_SHORT).show();
}

// here
}

我猜基于logcat选择文件没有问题。但是...
$ b $ pre $ 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);

//获取文件名
字符串fileNameSegments [] = filePath.split(/);
fileName = fileNameSegments [fileNameSegments.length - 1];

//将其转换为字节
byte [] fileByte = fileName.getBytes();

//创建ParseFile
ParseFile文件= new ParseFile(fileName,fileByte);

//将图片上传到Parse Cloud
file.saveInBackground();

//在Parse
中创建一个名为ImageUpload的新类ParseObject fileupload = new ParseObject(FileUpload);

//创建一个名为ImageName的列并设置字符串
fileupload.put(FileName,fileName);

//创建一个名为ImageFile的列并插入图片
fileupload.put(DocFile,file);

//创建类和列
fileupload.saveInBackground();

//显示一个简单的吐司消息
Toast.makeText(MainActivity.this,File Uploaded,Toast.LENGTH_SHORT).show();

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



当我点击.pdf文件后,触发了出错了但我找不到是什么原因。



编辑:
转换文件后抛出空指针异常路径到字节,当我删除try catch块时,应该是这样的。

解决方案

b $ b

  Uri uri = data.getData(); 
//获取路径
filePath = uri.getPath();

//获取文件名
字符串fileNameSegments [] = filePath.split(/);
fileName = fileNameSegments [fileNameSegments.length - 1];

//将其转换为字节
byte [] fileByte = fileName.getBytes();

//创建ParseFile
ParseFile文件= new ParseFile(fileName,fileByte);

//将文件上传到Parse Cloud
file.saveInBackground();

//在Parse $ b $中创建一个名为FileUpload的新类ParseObject fileUpload = new ParseObject(FileUpload);

//创建一个名为FileName的列并设置字符串
fileUpload.put(FileName,fileName);

Log.d(TAG,image file);
//创建一个名为ImageFile的列并插入图片
fileUpload.put(DocFile,file);

//创建类和列
fileUpload.saveInBackground();

Log.d(TAG,toast);
//显示一个简单的吐司消息
Toast.makeText(MainActivity.this,File Uploaded,
Toast.LENGTH_SHORT).show();

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


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

解决方案

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.

这篇关于解析:无法上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆