【问题标题】:Uploading files to Parse database将文件上传到 Parse 数据库
【发布时间】:2016-12-11 18:15:32
【问题描述】:

我想向我的应用程序添加一项功能,用户可以在其中将文件(PDF 文件)从他们的手机上传到数据库,然后将此文件下载回应用程序并显示它。

我不知道如何开始执行此操作以及使用正确的代码。

我尝试使用此代码,

ParseObject pObject = new ParseObject("ExampleObject");
  pObject.put("myNumber", number);
  pObject.put("myString", name);
  pObject.saveInBackground(); // asynchronous, no callback

- 编辑-

我尝试了这段代码,但是当我点击按钮时应用程序崩溃了:

public class Test extends Activity {

    Button btn;
    File PDFFile;
    ParseObject po;
    String userPDFFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);

       po = new ParseObject("pdfFilesUser");
        btn = (Button) findViewById(R.id.button);
        PDFFile = new File("res/raw/test.pdf");

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            uploadPDFToParse(PDFFile, po, userPDFFile);
            }
        });
    }

    private ParseObject uploadPDFToParse(File PDFFile, ParseObject po, String columnName){

            if(PDFFile != null){
            Log.d("EB", "PDFFile is not NULL: " + PDFFile.toString());
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            BufferedInputStream in = null;
            try {
                in = new BufferedInputStream(new FileInputStream(PDFFile));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            int read;
            byte[] buff = new byte[1024];
            try {
                while ((read = in.read(buff)) > 0)
                {
                    out.write(buff, 0, read);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                out.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }
            byte[] pdfBytes = out.toByteArray();

            // Create the ParseFile
            ParseFile file = new ParseFile(PDFFile.getName() , pdfBytes);
            po.put(columnName, file);

            // Upload the file into Parse Cloud
            file.saveInBackground();
            po.saveInBackground();
        }
        return po;
    }



    }

【问题讨论】:

标签: java android parse-platform back4app


【解决方案1】:

您可以通过REST API 手动上传file。看看这个文档here

可以试试这个代码:

private ParseObject uploadPDFToParse(File PDFFile, ParseObject po, String columnName){

if(PDFFile != null){
    Log.d("EB", "PDFFile is not NULL: " + PDFFile.toString());
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    BufferedInputStream in = null;
    try {
        in = new BufferedInputStream(new FileInputStream(PDFFile));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    int read;
    byte[] buff = new byte[1024];
    try {
        while ((read = in.read(buff)) > 0)
        {
            out.write(buff, 0, read);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    try {
        out.flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] pdfBytes = out.toByteArray();

    // Create the ParseFile
    ParseFile file = new ParseFile(PDFFile.getName() , pdfBytes);
    po.put(columnName, file);

    // Upload the file into Parse Cloud
    file.saveInBackground();
    po.saveInBackground();
}
return po;
}

更多详情请查看this

【讨论】:

  • 非常感谢您的回答。该链接确实帮助我理解 Parse 对象和文件。我现在知道如何使用它们了。但我仍然不明白的是,在执行所有这些操作之前,我应该将 pdf 文件存储到哪里(我正在使用 android studio)?我的意思是这个变量里面是什么:“PDFFile”?怎么把我电脑里的pdf文件保存在里面?
  • @DeemaAl-Shami "我怎样才能把我电脑里的 pdf 文件保存在里面?" - 你不能。该 PDF 位于您的计算机上,而不是您尝试从中上传的 Android 设备上
  • 那么这段代码存储的是什么?它存储的pdf文件在哪里? @cricket_007
  • @DeemaAl-Shami 您提出的所有问题都是“如何将 PDF 发送到 Parse”。第一个参数File PDFFile 允许您传递Android 设备硬盘驱动器中的任何文件,该驱动器与您的计算机完全分离。
  • 啊,是的,我现在明白了。非常感谢^^
【解决方案2】:

我强烈建议您快速熟悉 Parse Java 开发 wiki。

回答你的问题。你想使用:

byte[] data = "Working at Parse is great!".getBytes();
ParseFile file = new ParseFile("resume.txt", data);

file.saveInBackground();

首先声明您的文件等,然后使用它保存它。但再一次,首先阅读指南以更好地了解您使用的框架。

https://parseplatform.github.io/docs/android/guide/

【讨论】:

  • 非常感谢您的回答。该链接确实帮助我理解 Parse 对象和文件。我很感激这个^^
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-12-24
  • 2017-04-18
  • 1970-01-01
  • 1970-01-01
  • 2020-07-07
  • 2013-08-15
相关资源
最近更新 更多