Try entering the code:
if (requestCode == SELECT_FILE && 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]); imgPath = cursor.getString(columnIndex); cursor.close(); }else if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK ) { imgPath = fileUri.getPath(); if (imgPath != null && !imgPath.isEmpty()) { encodeImagetoString(); } else { Toast.makeText(getApplicationContext(), getResources().getString(R.string.some_error_occured), Toast.LENGTH_LONG).show(); } }
encodeImagetoString ()
public void encodeImagetoString() { new AsyncTask<Void, Void, String>() { protected void onPreExecute() { }; @Override protected String doInBackground(Void... params) { BitmapFactory.Options options = null; options = new BitmapFactory.Options(); options.inSampleSize = 3; bitmap = BitmapFactory.decodeFile(imgPath, options); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream); byte[] byte_arr = stream.toByteArray(); encodedString = Base64.encodeToString(byte_arr, 0); return ""; } @Override protected void onPostExecute(String msg) { Log.e("Success" , "Success"); } }.execute(null, null, null); }
Hope this helps you.
source share