Загрузка изображения в android в базу данных без кодирования
Мне нужна помощь, чтобы исправить эту проблему, я хочу загрузить изображение в android в базу данных без кодировки, вот мой код
sakit.java
package com.example.eis2; import androidx.annotation.RequiresApi; import androidx.appcompat.app.AppCompatActivity; import android.app.Activity; import android.app.DatePickerDialog; import android.app.ProgressDialog; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.provider.MediaStore; import android.util.Base64; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CalendarView; import android.widget.DatePicker; import android.widget.EditText; import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.Spinner; import android.widget.Toast; import com.android.volley.AuthFailureError; import com.android.volley.Request; import com.android.volley.RequestQueue; import com.android.volley.Response; import com.android.volley.VolleyError; import com.android.volley.toolbox.StringRequest; import com.android.volley.toolbox.Volley; import com.example.eis2.Item.getNo_pengajuan_full; import com.example.eis2.Item.getNo_pengajuan_non_full; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import static com.example.eis2.Item.LoginItem.KEY_NIK; import static com.example.eis2.izin.txt_nomor; import static com.example.eis2.izinfull.txt_lokasi; public class sakit extends AppCompatActivity { private Spinner spinner; ArrayList<String> Karyawan; EditText nopengajuan, editText, keterangan; Button pengajuan, upload; private List<getNo_pengajuan_full> no_pengajuan; ProgressDialog pDialog; final int CAMERA_REQUEST = 1888; ImageView upload_gambar; Bitmap bitmap; ImageButton add; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sakit); final String nik_baru = getIntent().getStringExtra(KEY_NIK); Karyawan = new ArrayList<>(); spinner = (Spinner) findViewById(R.id.karyawan); nopengajuan = (EditText) findViewById(R.id.nopengajuan); editText = (EditText) findViewById(R.id.tanggal); keterangan = (EditText) findViewById(R.id.keterangan_text); upload_gambar = (ImageView) findViewById(R.id.gambar1); no_pengajuan = new ArrayList<>(); pengajuan = (Button) findViewById(R.id.pengajuanfull); final EditText tanggal = new EditText(sakit.this); final LinearLayout container = (LinearLayout) findViewById(R.id.container); add = (ImageButton) findViewById(R.id.add); nopengajuan.setFocusable(false); final Calendar today = Calendar.getInstance(); final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener(){ @Override public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) { today.set(Calendar.YEAR, year); today.set(Calendar.MONTH, monthOfYear); today.set(Calendar.DAY_OF_MONTH, dayOfMonth); updateLabel(); } private void updateLabel() { String myFormat = "yyyy-MM-dd"; //In which you need put here SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US); editText.setText(sdf.format(today.getTime())); } }; editText.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub new DatePickerDialog(sakit.this, date, today .get(Calendar.YEAR), today.get(Calendar.MONTH), today.get(Calendar.DAY_OF_MONTH)).show(); } }); pengajuan.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { postfull(); } }); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { String karyawan = spinner.getItemAtPosition(spinner.getSelectedItemPosition()).toString(); Toast.makeText(getApplicationContext(), karyawan, Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); getLokasi(); getNo(); upload = (Button) findViewById(R.id.upload); upload.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intentGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intentGallery, CAMERA_REQUEST); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK && data != null){ Uri path = data.getData(); try { bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), path); upload_gambar.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); } Toast.makeText(sakit.this, "gambar sudah di upload", Toast.LENGTH_SHORT).show(); } } private void getLokasi() { String lokasi = txt_lokasi.getText().toString().trim(); StringRequest stringRequest = new StringRequest(Request.Method.GET, "http://hrd.tvip.co.id/rest_server/master/karyawan/index?lokasi_struktur=" + lokasi, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject jsonObject = new JSONObject(response); if (jsonObject.getString("status").equals("true")) { JSONArray jsonArray = jsonObject.getJSONArray("data"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObject1 = jsonArray.getJSONObject(i); String karyawan = jsonObject1.getString("nama_karyawan_struktur"); String nik = jsonObject1.getString("nik_baru"); Karyawan.add(karyawan + "|" + nik); } } spinner.setAdapter(new ArrayAdapter<String>(sakit.this, android.R.layout.simple_spinner_dropdown_item, Karyawan)); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_SHORT).show(); } }); RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest); } private void getNo() { pDialog = new ProgressDialog(this); showDialog(); pDialog.setContentView(R.layout.progress_dialog); pDialog.getWindow().setBackgroundDrawableResource( android.R.color.transparent ); StringRequest stringRequest1 = new StringRequest(Request.Method.GET, "http://10.12.1.75/db_eis/izin_full_day.php", new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject obj = new JSONObject(response); JSONArray movieArray = obj.getJSONArray("data"); ///get last data only/// for (int i = 0; i < movieArray.length(); i++) { JSONObject movieObject = movieArray.getJSONObject(i); getNo_pengajuan_full nomor = new getNo_pengajuan_full( movieObject.getInt("no_pengajuan_full_day")); no_pengajuan.add(nomor); } getNo_pengajuan_full item = no_pengajuan.get(no_pengajuan.size() - 1); nopengajuan.setText(String.valueOf(item.getNo_pengajuan_full() + 1)); hideDialog(); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Toast.makeText(getApplicationContext(), "Maaf, ada kesalahan", Toast.LENGTH_SHORT).show(); } }); RequestQueue requestQueue1 = Volley.newRequestQueue(this); requestQueue1.add(stringRequest1); } private void postfull(){ pDialog = new ProgressDialog(this); showDialog(); pDialog.setContentView(R.layout.progress_dialog); pDialog.getWindow().setBackgroundDrawableResource( android.R.color.transparent ); StringRequest stringRequest2 = new StringRequest(Request.Method.POST, "http://10.12.1.75/db_eis/izin_full_insert.php", new Response.Listener<String>() { @Override public void onResponse(String response) { hideDialog(); Toast.makeText(getApplicationContext(), "data sudah dimasukkan", Toast.LENGTH_LONG).show(); }}, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { hideDialog(); Toast.makeText(getApplicationContext(), "Maaf ada kesalahan", Toast.LENGTH_LONG).show(); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<String, String>(); String pengajuan = nopengajuan.getText().toString(); String nik_baru = getIntent().getStringExtra(KEY_NIK); String tanggal = editText.getText().toString(); String jabatan = txt_nomor.getText().toString(); String gambar = upload_gambar.toString(); String karyawan = spinner.getSelectedItem().toString(); String keterangan_sakit = keterangan.getText().toString(); params.put("no_pengajuan_full_day", pengajuan); params.put("nik_full_day", nik_baru); params.put("jabatan_full_day", jabatan); params.put("jenis_full_day","SA"); params.put("start_full_day", tanggal); params.put("karyawan_pengganti", karyawan); params.put("ket_tambahan", keterangan_sakit); params.put("status_full_day","0"); params.put("status_full_day_2","0"); params.put("upload_full_day", gambar); return params; } }; RequestQueue requestQueue2 = Volley.newRequestQueue(this); requestQueue2.add(stringRequest2); } private void showDialog() { if (!pDialog.isShowing()) pDialog.show(); } private void hideDialog() { if (pDialog.isShowing()) pDialog.dismiss(); } }
а вот и php для загрузки изображения
<pre><?php include 'connect2.php'; $tanggal = date('Y-m-d'); $no_pengajuan_full_day = $_POST['no_pengajuan_full_day']; $nik_full_day = $_POST['nik_full_day']; $jabatan_full_day = $_POST['jabatan_full_day']; $jenis_full_day = $_POST['jenis_full_day']; $start_full_day = $_POST['start_full_day']; $karyawan_pengganti = $_POST['karyawan_pengganti']; $ket_tambahan = $_POST['ket_tambahan']; $status_full_day = $_POST['status_full_day']; $status_full_day_2 = $_POST['status_full_day_2']; $upload_full_day = $_FILES['upload_full_day']['name']; if (strlen($upload_full_day)>0) { //upload Photo if (is_uploaded_file ($_FILES['upload_full_day']['tmp_name'])) { move_uploaded_file ($_FILES['upload_full_day']['tmp_name'], "C:/xampp/htdocs/eis/uploads/izin/full_day/".$upload_full_day); } } $sql = "INSERT INTO tbl_izin_full_day (no_pengajuan_full_day, nik_full_day, jabatan_full_day, jenis_full_day, start_full_day, karyawan_pengganti, ket_tambahan, status_full_day, status_full_day_2, upload_full_day) VALUES ( '$no_pengajuan_full_day', '$nik_full_day', '$jabatan_full_day', '$jenis_full_day', '$start_full_day', '$karyawan_pengganti', '$ket_tambahan', '$status_full_day', '$status_full_day_2', '$upload_full_day' )"; $result = mysqli_query($conn, $sql); ?>
Что я уже пробовал:
Я загрузил в android, но в моей базе данных и папке не появляется ни одной картинки
но, когда я тестировал php-файл в postman, он появляется
Sandeep Mewara
Какая кодировка? - Куда же?
Komang Putra
я имею в виду base64 для кодирования изображения
но я не хочу этим пользоваться..
Итак, я удаляю его