Search code examples
androidcamerasd-cardimage-uploading

Upload selected images from SD card to a php server


In my android application, I am able to capture an image and store in SD card. I have a select button and check boxes to select pictures. But I don't know how to upload the selected images to a php server to display via my website. Code is posted below, Please help by telling how to upload those selected images. Thank you

imageAdapter = new ImageAdapter();
imageAdapter.initialize();
imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(imageAdapter);
final Button selectBtn = (Button) findViewById(R.id.selectBtn);
selectBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
  final int len = imageAdapter.images.size();
int cnt = 0;
  String selectImages = "";
                for (int i = 0; i < len; i++) {
                    if (imageAdapter.images.get(i).selection) {
                        cnt++;
                        selectImages = selectImages
                                + imageAdapter.images.get(i).id + ",";
                    }
                }
                if (cnt == 0) {
                    Toast.makeText(getApplicationContext(),
                            "Please select at least one image",
                            Toast.LENGTH_LONG).show();
                } else {
                    selectImages = selectImages.substring(0,selectImages.lastIndexOf(","));
                    Intent intent = new Intent(MainActivity.this,
                            UploadQueue.class);
                    intent.putExtra("Ids", selectImages);

                    startActivityForResult(intent, UPLOAD_IMAGES);
                }

Solution

  • 1) Create a webservice on your server

    2) convert your image to Base64 string in android

    3) send that string to the webservice by ksoap2

    4) convert back the string to image in webservice (You do not need to convert it to Image File if not needed)

    5) save it on hard disk of the server

    Edit:

    public static Bitmap base64ToBitmap(String strBase64) throws IOException {
            byte[] bitmapdata = Base64.decode(strBase64);
            Bitmap bitmap = BitmapFactory.decodeByteArray(bitmapdata, 0,
                    bitmapdata.length);
            return bitmap;
        }
    
    public static String bitmapToBase64(Bitmap bitmap) {
            byte[] bitmapdata = bitmapToByteArray(bitmap);
            return Base64.encodeBytes(bitmapdata);
        }
    
    public static byte[] bitmapToByteArray(Bitmap bitmap) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.PNG, 0 /* ignored for PNG */, bos);
        byte[] bitmapdata = bos.toByteArray();
        return bitmapdata;
    }
    

    or

        public static byte[] fileToByteArray(String path) throws IOException {
            File imagefile = new File(path);
            byte[] data = new byte[(int) imagefile.length()];
            FileInputStream fis = new FileInputStream(imagefile);
            fis.read(data);
            fis.close();
            return data;
        }
    
    
    public static String fileToBase64(String path) throws IOException {
            byte[] bytes = fileToByteArray(path);
            Base64.encodeBytes(bytes);
        }
    
    
    public static void base64ToFile(String path, String strBase64)
                throws IOException {
            byte[] bytes = Base64.decode(strBase64);
            byteArrayTofile(path, bytes);
        }
    
    public static void byteArrayTofile(String path, byte[] bytes)
                throws IOException {
            File imagefile = new File(path);
            File dir = new File(imagefile.getParent());
            if (!dir.exists()) {
                dir.mkdirs();
            }
            FileOutputStream fos = new FileOutputStream(imagefile);
            fos.write(bytes);
            fos.close();
        }