dqtdz08206 2018-04-06 03:28
浏览 197

JSON在Android中解码来自Android的base64图像,然后上传到数据库

I want to upload an image from android along with other data to a database via PHP. I used varchar in the database that saves the image data. The android app will decode the image into base64 and send the data to the PHP where it will decode the image, rename it, and then save it in the database. I also got an error on the android that says

org.json.JSONException: Value br of type java.lang.String cannot be converted to JSONObject

here is my Android java:

public class homepageDosen extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

    EditText txJurnalName;
    Spinner ddPublikasi,ddStatus;
    ImageView imagePreview;
    String Dosen ;

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

        SharedPreferences DataDosen = getSharedPreferences("Dosen", Context.MODE_PRIVATE);
        Dosen = DataDosen.getString("kodeDosen","");

        txJurnalName = (EditText) findViewById(R.id.txJurnalName);
        imagePreview = (ImageView) findViewById(R.id.imagePreview);
        ddPublikasi = (Spinner) findViewById(R.id.ddPublikasi) ;
        ddStatus = (Spinner) findViewById(R.id.ddStatus);
        ArrayAdapter<CharSequence> publikasi = ArrayAdapter.createFromResource(this, R.array.tipe_publikasi,android.R.layout.simple_spinner_item);
        ArrayAdapter<CharSequence> status = ArrayAdapter.createFromResource(this, R.array.status,android.R.layout.simple_spinner_item);
        publikasi.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        status.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        ddPublikasi.setAdapter(publikasi);
        ddStatus.setAdapter(status);

        ddPublikasi.setOnItemSelectedListener(this);
        ddStatus.setOnItemSelectedListener(this);

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    public void Search(View view) {
        Intent photoPickerIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(photoPickerIntent,Utils.REQCODE);
    }

    public void Upload(View view) {


        if(imagePreview.getDrawable()==(null)){
            Toast.makeText(this,"Pilih gambar dari galeri",Toast.LENGTH_SHORT).show();
        }
        else if(txJurnalName.getText().toString().isEmpty() ){
            Toast.makeText(this,"Masukan nama jurnal atau konfrensi",Toast.LENGTH_SHORT).show();
        }
        else{
//            Intent intent2 = getIntent();
//            String Dosen = intent2.getStringExtra("kodeDosen");
            Bitmap image = ((BitmapDrawable) imagePreview.getDrawable()).getBitmap();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            image.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
            String encodedImage = Base64.encodeToString(byteArrayOutputStream.toByteArray(), Base64.DEFAULT);
            String publikasi = ddPublikasi.getSelectedItem().toString();
            String status = ddStatus.getSelectedItem().toString();
            JSONObject upload = new JSONObject();
            try{
                upload.put("kodeDosen",Dosen);
                upload.put("imagePreview",encodedImage);
                upload.put("namaJurnal",txJurnalName.getText().toString().trim());
                upload.put("tipePublikasi",publikasi);
                upload.put("status",status);
            }
            catch (JSONException e){
                e.printStackTrace();
            }
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, Utils.uploadURL,upload, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Toast.makeText(homepageDosen.this,"Image was uploaded succesfully",Toast.LENGTH_SHORT).show();


                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText(homepageDosen.this, "error"+error.toString(), Toast.LENGTH_LONG).show();
                }
            });
            jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(5000,DefaultRetryPolicy.DEFAULT_MAX_RETRIES,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            Volley.newRequestQueue(this).add(jsonObjectRequest);
        }


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == Utils.REQCODE && resultCode == RESULT_OK && data != null) {
            Uri selectedImageUri = data.getData();
            imagePreview.setImageURI(selectedImageUri);
        }
    }
}

this is my php(uploadDosen.php)

the rename format is:kodeDosen_microtime.jpg
where kodeDosen is a input variable (ex:D0000) and microtime is the timestamp.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 汇编语言除法溢出问题
    • ¥50 C++实现删除N个数据列表共有的元素
    • ¥15 Visual Studio问题
    • ¥15 state显示变量是字符串形式,但是仍然红色,无法引用,并显示类型不匹配
    • ¥20 求一个html代码,有偿
    • ¥100 关于使用MATLAB中copularnd函数的问题
    • ¥20 在虚拟机的pycharm上
    • ¥15 jupyterthemes 设置完毕后没有效果
    • ¥15 matlab图像高斯低通滤波
    • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗