I am a newbie in android development. I am doing a project where I have to upload data with an image attached. I developed an app which is working fine in android version 5.1.1 and below but crashes for version 6.0 and above. I am using retrofit 2.4 and room database. I am storing the real path of the image URI and then in a separate process, I am uploading image and data reading from room database.
here is the saving image path code.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SELECT_PICTURE && resultCode == RESULT_OK) {
selectedImageUri = data.getData();
vispic = RealPathUtils.getRealPathFromURI_API19(getContext(), selectedImageUri);
Toast.makeText(getActivity(), vispic, Toast.LENGTH_LONG).show();
}
}
this function where URI getting converted to RealPath
public static String getRealPathFromURI_API19(Context context, Uri uri){
String filePath = "";
String wholeID = DocumentsContract.getDocumentId(uri);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
return filePath;
}
here I am converting realpath to URI to requestbody
for (int i = 0; i < ListofData.size(); i++) {
idx = i ;
Uri uri = Uri.fromFile(new File(ListofData.get(i).getVisit_pic()));
imageView.setImageURI(uri);
RequestBody requestFile = RequestBody.create(MediaType.parse("image/jpeg"), convertImageToByte(uri));
MultipartBody.Part visit_pic = MultipartBody.Part.createFormData("visit_pic", "image.jpg", requestFile);
here the app keeps crashing...please someone help me...