使用uri i/o错误上传多种类型的文件以解析数据库

问题描述

我有一个使用 Parse-Server 作为后端的应用程序,我通过 back4app 使用它。 parse-server 提供的数据库是我的应用程序的数据库

我正在关注以下文档 https://docs.parseplatform.org/android/guide/#files

我的应用在一个片段中有多个上传文件按钮。我的应用程序接受 .pdf、图像文件和 .doc 文件。但是,当我尝试使用 saveDataInBackground() 保存新创建的解析文件时,出现 I/O 错误

我的代码如下:

在我的片段中,我使用 onActivityResult 进行检索

  public void onActivityResult(int requestCode,int resultCode,@Nullable Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    binding.cross1.setVisibility(View.VISIBLE);

                    Uri uri = data.getData();
                    String uriString = uri.toString();
                    File myFile = new File(uriString);

                    parseFiles[0] = (new ParseFile(myFile));

                    String displayName = null;

                    if (uriString.startsWith("content://")) {
                        Cursor cursor = null;
                        try {
                            cursor = getActivity().getContentResolver().query(uri,null,null);
                            if (cursor != null && cursor.movetoFirst()) {
                                displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.disPLAY_NAME));
                            }
                        } finally {
                            cursor.close();
                        }
                    } else if (uriString.startsWith("file://")) {
                        displayName = myFile.getName();
                    }

                    binding.file1.setText(displayName);
                }
                break;
            case 2:
                if (resultCode == RESULT_OK) {
                    binding.cross2.setVisibility(View.VISIBLE);
                    Uri uri = data.getData();
                    String uriString = uri.toString();
                    File myFile = new File(uriString);

                    parseFiles[1] = (new ParseFile(myFile));

                    String displayName = null;

                    if (uriString.startsWith("content://")) {
                        Cursor cursor = null;
                        try {
                            cursor = getActivity().getContentResolver().query(uri,null);
                            if (cursor != null && cursor.movetoFirst()) {
                                displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.disPLAY_NAME));
                            }
                        } finally {
                            cursor.close();
                        }
                    } else if (uriString.startsWith("file://")) {
                        displayName = myFile.getName();
                    }

                    binding.file2.setText(displayName);
                }
                break;
            case 3:
                if (resultCode == RESULT_OK) {
                    binding.cross3.setVisibility(View.VISIBLE);
                    Uri uri = data.getData();
                    String uriString = uri.toString();
                    File myFile = new File(uriString);

                    parseFiles[2] = (new ParseFile(myFile));

                    String displayName = null;

                    if (uriString.startsWith("content://")) {
                        Cursor cursor = null;
                        try {
                            cursor = getActivity().getContentResolver().query(uri,null);
                            if (cursor != null && cursor.movetoFirst()) {
                                displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.disPLAY_NAME));
                            }
                        } finally {
                            cursor.close();
                        }
                    } else if (uriString.startsWith("file://")) {
                        displayName = myFile.getName();
                    }

                    binding.file3.setText(displayName);
                }
                break;
        }
    }

在我的活动中,我试图将 parseFiles 保存为这样。

 ArrayList<ParseFile> parseFiles = new ArrayList<>();


            for (int i = 0; i < adapter.getFragmentJobSample().getParseFiles().length; i++) {
                if (adapter.getFragmentJobSample().getParseFiles()[i] != null){
                    parseFiles.add(adapter.getFragmentJobSample().getParseFiles()[i]);
                }
            }


            entity.put("fileOne",parseFiles.get(0));
            entity.put("fileTwo",parseFiles.get(1));
            entity.put("fileThree",parseFiles.get(2));

            for (int i = 0; i < parseFiles.size(); i++) {
                try {
                    parseFiles.get(i).save();
                } catch (ParseException e) {
                    Toast.makeText(this,"e" + e.getMessage(),Toast.LENGTH_SHORT).show();
                }
            }

            // Saves the new object.
            // Notice that the SaveCallback is totally optional!
            entity.saveInBackground(e -> {
                if (e == null) {
               

 //Save was done
                Toast.makeText(this,"success!",Toast.LENGTH_SHORT).show();
                finish();
            } else {
                //Something went wrong
                Toast.makeText(this,e.getMessage(),Toast.LENGTH_SHORT).show();
            }
        });

我不得不使用 .save() 因为我需要一次保存多个文件。我认为使用 .saveInBackground() 对此并不明智。我应该改用 .saveInBackground() 吗?

解决方法

您需要先将其转换为字节数组。为此,请查看以下链接: Image Uri to bytesarray

之后,只需将其与您的文件相关联,如下所示:

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

然后尝试保存此文件,它应该可以工作。