0

I am working on a module in which user can upload the image to the server. To achieve this, I have to change selected image into Base64. After conversion, I have to use Json POST method to upload image, but every time application crashes and I am getting error in this line..

String ba1=Base64.encodeToString(ba,Base64.DEFAULT);

this is my code that I am trying, PLease have a look and let me what mistake I am doing here.

buttonLoadPicture.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent i = new Intent(
                    Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                    startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        ss = BitmapFactory.decodeFile(picturePath);
        Log.d("value", ss.toString());

  Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),ss);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 100, bao);
byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba,Base64.DEFAULT);
Devraj
  • 1,479
  • 1
  • 22
  • 42

1 Answers1

1

encodeToString is not a function in BitmapFactory. You should encode to base64 in a different way. I would like to suggest this answer

Community
  • 1
  • 1
TmKVU
  • 2,910
  • 2
  • 16
  • 30
  • Thank u for your help buddy, I have tried your suggested example. It is converting the bitmap into base64. But it is showing so many lines for Log.e("LOOK", imageEncoded); – Devraj Jul 03 '15 at 12:55
  • You can just remove the line `Log.e("LOOK", imageEncoded);`. It won't make any difference for the functionality. – TmKVU Jul 03 '15 at 12:58
  • yeah!!.. that is right buddy. But my question is all about the many lines. I mean I have selected only one image but it is giving me so many base64 code lines. – Devraj Jul 03 '15 at 13:05
  • @Devraj of course it will. Images generally consist of relatively many bytes. If one encodes these bytes using Base64 one will receive many characters. Every byte consists of 8 bits and every Base64 character consists of 7 bits, hence the amount of characters. – TmKVU Jul 03 '15 at 13:08
  • Brother, Now I have a another problem, when I am trying to POST Image on server, application goes crash. I have updated my question, Please have a look. – Devraj Jul 03 '15 at 14:00
  • @Devraj That is not how SO works. If you have solved this problem you should accept the answer and ask a separate question for a new problem, but try to figure it out yourself first. You will learn more from that. – TmKVU Jul 04 '15 at 08:14
  • Sure buddy, I will ask a new question for my new query. Thank you buddy for your help. I am voting up your answer as it worked for me. – Devraj Jul 06 '15 at 06:04