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

时间:2018-04-06 03:28:34

标签: php android json database base64

我想通过PHP将android中的图像以及其他数据上传到数据库。我在保存图像数据的数据库中使用了varchar。 Android应用程序将图像解码为base64并将数据发送到PHP,它将解码图像,重命名,然后将其保存在数据库中。我在android上也出现了错误

  

org.json.JSONException:java.lang.String类型的值br不能   转换为JSONObject

这是我的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);
        }
    }
}

这是我的php(uploadDosen.php)

重命名格式为:kodeDosen_microtime.jpg
其中kodeDosen是输入变量(例如:D0000),microtime是时间戳。

0 个答案:

没有答案