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.