Skip to content
Advertisement

whats is the best/fastest way to upload images from android to mysql?

whats is the best/fastest way to upload images from android to mysql?

the problem that im having that uploading images encoded in base46 that it takes almost 10sec and after 3 or 4 uploads using bitmap the app crashes sometimes because bitmap other because of the encoded images on old android phone.. right now im using ION but i guess ion isnt the best right now as 10sec i find it alot and on more question should i send them as List, JSON array, JSON array then JSON object or params ?

bitmap Error which is only happen after 3 or 4 successful uploads

   : Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference

image selection

ActivityResultLauncher<Intent> mgetcontent = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {

                if (result.getResultCode() == Activity.RESULT_OK) {

                    Intent data = result.getData();
                    ClipData clipData = data.getClipData();
                    if (clipData != null) {
                        for (int i = 0; i < clipData.getItemCount(); i++) {

                            Uri imageUri2 = clipData.getItemAt(i).getUri();

                            int j =0;

                                bitmap = new Bitmap[i];
                            while (j < i) {
                                imageUri2 = data.getClipData().getItemAt(j).getUri();
                                try {
                                    bitmap[j] = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri2);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                                j++;

                            }

                            picsModelClassList.add(new PicsModelClass(
                                    imageUri2.toString()
                            ));
                            PicsAdapter adapter = new PicsAdapter(picsModelClassList, getApplicationContext());
                            adapter.notifyDataSetChanged();
                            pics_recycler.setAdapter(adapter);
                        }
                    }else {
                        Uri uri = data.getData();
                        picsModelClassList.add(new PicsModelClass(
                                uri.toString()
                        ));
                        PicsAdapter adapter = new PicsAdapter(picsModelClassList, getApplicationContext());
                        adapter.notifyDataSetChanged();
                        pics_recycler.setAdapter(adapter);
                    }

                }
            }});

encode images

   public String getStringImage(Bitmap bmp){
       ByteArrayOutputStream baos = new ByteArrayOutputStream();
       bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
       byte[] imageBytes = baos.toByteArray();
       String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
       return encodedImage;
   }

php

     $pics_array = $_POST['pics_array'];
     $ImagePath = pics/';
     file_put_contents($ImagePath.uniqid().'.jpg',base64_decode($pics_array));

Advertisement

Answer

Edit: reread your code, you are compressing to a PNG. So it just leaves one issue

Don’t use base64. It’s binary data, send it as binary data. Sending it as Base64 will always increase it to 2-3x the size. Base64 should only be used where you absolutely have to, its a hack to send binary data over text only channels like URLs.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement