如何在安卓中从相册上传图片到Parse服务器?

3

这是我的代码

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



}

这是我的日志响应:

08-29 19:23:28.569 7072-7072/com.appcentricsoftware.bumbysdigital W/System.err: com.parse.ParseRequest$ParseRequestException: 坏的json响应 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.ThreadPoolExecutor$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: 类型为java.lang.String的值无法转换为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

“String cannot be converted to JSONObject”... 我在这里的代码中没有看到任何JSONObject。 - OneCricketeer
我只是使用了saveInBackground()和新的保存回调来保存ParseFile并遇到了这个问题。 - Deepshikha Puri
1个回答

0

将此行更改为

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

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

你添加了这些行吗?

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

我没有特定的照片表。我需要将这张图片与其他一些参数一起保存在另一个表中。如果不保存解析文件,则会显示无法编码未保存的解析文件。 - Deepshikha Puri

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接