上传多张图片到解析服务器android java

blmhpbnm  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(337)

我正在编写一个android应用程序,其中包括选择多个图像并将它们上传到解析服务器。我已经实现了多图像选择使用电报安卓画廊采集器。我的问题发生在将图像转换为位图并保存以进行解析时。我得到以下错误:
java.lang.runtimeexception:未能将结果resultinfo{who=null,request=2,result=-1,data=intent{(有附加)}}传递到活动{[package]/[package].mainactivity}:java.lang.nullpointerexception:尝试对null对象引用调用虚拟方法“java.io.file.com.parse.parseplugins.getparsedir()”

原因:java.lang.nullpointerexception:尝试对空对象引用调用虚拟方法“java.io.file com.parse.parseplugins.getparsedir()”
欢迎所有输入!谢谢。
这是我的密码:

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

    }
}

编辑-实际上有两个错误与相同的错误:-如果图像是从手机,它得到的解析行和应用程序崩溃,-如果图像是从sd卡它崩溃之前。啊
编辑2-我修复了编辑1中的错误,所以现在100%的东西不能用解析了!

wtzytmuj

wtzytmuj1#

你初始化了解析吗?

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

相关问题