0

I am working in application in which I want user to select multiple and convert them to base64, so that I can sent it to server. Is it possibile to select multiple images from gallery and then convert them to base64 and then send it to server

            Intent intent = new Intent();
            intent.setType("*/*");


            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
                intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
            }
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "android.intent.action.SEND_MULTIPLE"), SELECT_PICTURE);



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub

    if (requestCode == SELECT_PICTURE) {

        if (resultCode == RESULT_OK) {
            //data.getParcelableArrayExtra(name);
            //If Single image selected then it will fetch from Gallery
            filePath = data.getData();

                filePath = data.getData();
                if (null != filePath) {
                    try {
                        bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
                      //  img.setImageBitmap(bitmap);
                        if (filePath.getScheme().equals("content")) {
                            Cursor cursor = getContentResolver().query(filePath, null, null, null, null);
                            try {
                                if (cursor != null && cursor.moveToFirst()) {
                                    file_name = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                                   // text.setText(file_name+",");
                                    img_name.add(file_name);
                                    img_pic.add(getStringImage(bitmap));
                                    //      Toast.makeText(this, "1." + file_name, Toast.LENGTH_SHORT).show();
                                }
                            } finally {
                                cursor.close();
                            }
                        } else {

                            String path = data.getData().getPath();
                            file_name = path.substring(path.lastIndexOf("/") + 1);
                           // text.setText(file_name);
                            img_name.add(file_name);
                            img_pic.add(getStringImage(bitmap));
                            //Toast.makeText(this, "2." + file_name, Toast.LENGTH_SHORT).show();
                        }


                    } catch (IOException e) {
                        e.printStackTrace();
                    }
  • 1
    Possible duplicate of [How to convert a image into Base64 string?](https://stackoverflow.com/questions/4830711/how-to-convert-a-image-into-base64-string) – Manoj Perumarath Oct 05 '17 at 03:45

1 Answers1

0

Sure you can either manage selecting and displaying the images yourself or you can rely on Androids File Intent chooser to let them select and return. You can then use the URIs provided to retrieve the images, convert and send.

Getting user selected images is simple so I won't post that, but just in case it is something you are not familiar with, here is a link that will walk you through it. Select multiple images from android gallery

Now converting to Base64 should be asynctask

Use the following:

public class Base64EncodeMediaAsyncTask extends AsyncTask<Void, Void, MediaModel> {

/*///////////////////////////////////////////////////////////////
// MEMBERS
*////////////////////////////////////////////////////////////////
private static final String TAG = Globals.SEARCH_STRING + Base64EncodeMediaAsyncTask.class.getSimpleName();
private MediaModel mMediaModelToConvert;


/*///////////////////////////////////////////////////////////////
// CONSTRUCTOR
*////////////////////////////////////////////////////////////////
public Base64EncodeMediaAsyncTask(MediaModel model){
    mContext = context;
    mMediaModelToConvert = model; //it's just a file wrapper, nothing special lol.

}


/*///////////////////////////////////////////////////////////////
// OVERRIDES
*////////////////////////////////////////////////////////////////
@Override
protected MediaModel doInBackground(Void... params) {
    try{
        InputStream inputStream = new FileInputStream(mMediaModelToConvert.getAbsoluteLocalPath());//You can get an inputStream using any IO API
        byte[] bytes;
        byte[] buffer = new byte[(int) new File(mMediaModelToConvert.getAbsoluteLocalPath()).length()];
        int bytesRead;

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        while ((bytesRead = inputStream.read(buffer)) != -1) {
            output.write(buffer, 0, bytesRead);
        }

        bytes = output.toByteArray();

        mMediaModelToConvert.setBase64String(Base64.encodeToString(bytes, Base64.DEFAULT));

    }catch (Exception ex){
        A35Log.e(TAG, "Failed to get base 64 encoding for file: " + mMediaModelToConvert.getAbsoluteLocalPath());
        return null;

    }

    return mMediaModelToConvert;

}
@Override
protected void onPostExecute(MediaModel success) {
    super.onPostExecute(success);

 }

}
Sam
  • 4,979
  • 1
  • 21
  • 35