将多张图片上传到 Parse Server Android Java

Upload multiple image to Parse Server Android Java

我正在编写一个 Android 应用程序,该应用程序涉及选择多个图像并将它们上传到解析服务器。 我已经使用 Telegram Android Gallery picker. 实现了多图像选择 当需要将图像转换为位图并将它们保存到 Parse 时,我的问题就出现了。我收到以下错误:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { (has extras) }} to activity {[package]/[package].MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File com.parse.ParsePlugins.getParseDir()' on a null object reference

欢迎所有意见!谢谢。

这是我的代码:

public class MainActivity extends AppCompatActivity {
    private Bitmap bitmap;

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 2) {
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {}
        }
    }

    public void pickImage(View view) {
        GalleryConfig config = new GalleryConfig.Build()
            .limitPickPhoto(8)
            .singlePhoto(false)
            .hintOfPick("this is pick hint")
            .filterMimeTypes(new String[] {
                "image/*"
            })
            .build();
        GalleryActivity.openActivity(MainActivity.this, 2, config);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        //list of photos of seleced
        List < String > photos = (List < String > ) data.getSerializableExtra(GalleryActivity.PHOTOS);

        if (photos != null) {
            for (String photo: photos) {
                // BitmapFactory.Options options = new BitmapFactory.Options();
                // options.inPreferredConfig = Bitmap.Config.ARGB_8888;

                bitmap = BitmapFactory.decodeFile(photo);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] image = stream.toByteArray();

                ParseFile file = new ParseFile("image.png", image);
                file.saveInBackground(new SaveCallback() {
                    @Override
                    public void done(ParseException e) {
                        if (e == null) {
                            Log.i("did it fail?", "no");
                        } else {
                            // Failed
                            Log.i("did it fail?", "yes");
                        }
                    }
                });

                ParseObject iupload = new ParseObject("ImageUpload");
                iupload.put("ImageName", "productImage");
                iupload.put("ImageFile", file);
                iupload.saveInBackground();
            }
            Log.i("Is it done?", "Yes");
        } else {
            Log.i("Is it done?", "nope");
        }

        //list of videos of seleced
        // List<String> vides = (List<String>) data.getSerializableExtra(GalleryActivity.VIDEOS);
    }

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

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                requestPermissions(new String[] {
                    Manifest.permission.READ_EXTERNAL_STORAGE
                }, 2);
            }
        }

    }
}

Edit - 实际上有两个错误相同的错误: - 如果图像来自 phone 它到达 Parse 行并且应用程序崩溃, - 如果图像来自 SD 卡,它会在此之前崩溃。 呃

编辑 2 - 我修复了编辑 #1 中的错误,所以现在它 100% 无法使用解析!

你初始化Parse了吗?

Parse.enableLocalDatastore(this);
Parse.initialize()
Parse.initialize(this, "APPLICATION_ID", "CLIENT_KEY");