简体   繁体   中英

How to Upload the image from gallery to Parse Server in android..?

Here is My code

public class TestingActivity extends AppCompatActivity {

Button btn_testing;
ProgressDialog pDialog;
ImageView iv_image;
Bitmap bMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testing);
    btn_testing = (Button)findViewById(R.id.btn_testing);
    iv_image = (ImageView)findViewById(R.id.iv_imagetesting);

    FileInputStream in;
    BufferedInputStream buf;


    try
    {
        in = new FileInputStream("/storage/emulated/0/Pictures/InstaTag_Edit_20160427_131230-346274175.jpg");
        buf = new BufferedInputStream(in,1070);
        System.out.println("1.................."+buf);
        byte[] bMapArray= new byte[buf.available()];
        buf.read(bMapArray);
         bMap = BitmapFactory.decodeByteArray(bMapArray, 0, bMapArray.length);
        iv_image.setImageBitmap(bMap);
        if (in != null)
        {
            in.close();
        }
        if (buf != null)
        {
            buf.close();
        }

    }
    catch (Exception e) {
        Log.e("Error reading file", e.toString());
    }
    btn_testing.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            pDialog = new ProgressDialog(TestingActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.show();


            Bitmap bitmap = ((BitmapDrawable)iv_image.getDrawable()).getBitmap();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            // Compress image to lower quality scale 1 - 100
            bMap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] image = stream.toByteArray();

            ParseFile file = new ParseFile("androidbegin.png", image,"image/png");
            file.saveInBackground(new SaveCallback() {
                @Override
                public void done(ParseException e) {
                    try {
                        pDialog.dismiss();
                    } catch (Exception e1) {
                    }
                    if (e == null) {
                        Toast.makeText(TestingActivity.this, "Image saved", Toast.LENGTH_LONG).show();
                    } else {
                        e.printStackTrace();
                        Toast.makeText(TestingActivity.this, "Image not saved", Toast.LENGTH_LONG).show();
                    }
                }
            });
        }
    });



}

}

Here is my Log Response:

08-29 19:23:28.569 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: com.parse.ParseRequest$ParseRequestException: bad json response 08-29 19:23:28.570 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at com.parse.ParseRequest.newTemporaryException(ParseRequest.java:290) 08-29 19:23:28.570 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at com.parse.ParseRESTCommand.onResponseAsync(ParseRESTCommand.java:308) 08-29 19:23:28.570 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at com.parse.ParseRequest$3.then(ParseRequest.java:137) 08-29 19:23:28.570 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at com.parse.ParseRequest$3.then(ParseRequest.java:133) 08-29 19:23:28.570 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at bolts.Task$15.run(Task.java:917) 08-29 19:23:28.570 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at bolts.BoltsExecutors$ImmediateExecutor.execute(BoltsExecutors.java:105) 08-29 19: 23:28.570 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at bolts.Task.completeAfterTask(Task.java:908) 08-29 19:23:28.570 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at bolts.Task.continueWithTask(Task.java:715) 08-29 19:23:28.570 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at bolts.Task.continueWithTask(Task.java:726) 08-29 19:23:28.570 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at bolts.Task$13.then(Task.java:818) 08-29 19:23:28.571 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at bolts.Task$13.then(Task.java:806) 08-29 19:23:28.571 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at bolts.Task$15.run(Task.java:917) 08-29 19:23:28.571 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 08-29 19:23:28.571 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at java.util.concurrent.ThreadPoolExe cutor$Worker.run(ThreadPoolExecutor.java:588) 08-29 19:23:28.571 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at java.lang.Thread.run(Thread.java:818) 08-29 19:23:28.571 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: Caused by: org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject 08-29 19:23:28.578 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at org.json.JSON.typeMismatch(JSON.java:111) 08-29 19:23:28.579 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at org.json.JSONObject.(JSONObject.java:160) 08-29 19:23:28.579 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at org.json.JSONObject.(JSONObject.java:173) 08-29 19:23:28.579 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: at com.parse.ParseRESTCommand.onResponseAsync(ParseRESTCommand.java:298) 08-29 19:23:28.579 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: ... 13 more

Change this line from

ParseFile file = new ParseFile("androidbegin.png", image,"image/png");

to

ParseFile file = new ParseFile("androidbegin.png", image);

And have you added these lines,

            ParseObject photo = new ParseObject("Photo");
            photo.put("fileFull", imgfile);
            photo.put("User", ParseUser.getCurrentUser());
            photo.saveInBackground();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM