I am implementing file uploading concept through gallery,camera and filemanager. Here is the code
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
// CALL THIS METHOD TO GET THE URI FROM THE BITMAP
Uri tempUri = getImageUri(getApplicationContext(), photo);
// CALL THIS METHOD TO GET THE ACTUAL PATH
File finalFile = new File(getRealPathFromURI(tempUri));
suppdocs.setText(strPath);
} else if (requestCode == 2) {
Bitmap bm=null;
if (data != null) {
try {
bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
Uri tempUri = getImageUri(getApplicationContext(), bm);
File finalFile = new File(getRealPathFromURI(tempUri));
suppdocs.setText(strPath);
} else if (requestCode == 3) {
Uri uri = data.getData();
strPath= getPath(getApplicationContext(),uri);
suppdocs.setText(strPath);
}
}
}
Here is the code to get path for selected file from filemanager
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/"
+ split[1];
}
} else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
} else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{split[1]};
return getDataColumn(context, contentUri, selection,
selectionArgs);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
//Here is the code of sending file to server
private void senddata1(final String path) {
if(common.isNetworkAvailable(getApplicationContext())) {
try {
Log.d("TestTag","filename : "+path+" file : "+strfile);
progressBar2.setVisibility(View.VISIBLE);
submit_btn.setVisibility(View.GONE);
File file = new File(path);
// Parsing any Media type file
RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());
RequestBody strgid = RequestBody.create(MediaType.parse("text/plain"),gid);
RequestBody key = RequestBody.create(MediaType.parse("text/plain"),strKey);
APIService getResponse = apiclient.imgUpload().create(APIService.class);
Call<ImageUpload> call = getResponse.uploadFile(fileToUpload,filename);
call.enqueue(new Callback<ImageUpload>() {
@Override
public void onResponse(Call<ImageUpload> call, Response<ImageUpload> response) {
progressBar2.setVisibility(View.GONE);
submit_btn.setVisibility(View.VISIBLE);
if (response.isSuccessful()) {
String message=response.body().getMessage();
String type=response.body().getType();
}
}
@Override
public void onFailure(Call<ImageUpload> call, Throwable t) {
Log.d("TestTag","throwable : "+t);
common.showtoast("Failed to upload image.",getApplicationContext());
}
});
} catch (Exception e) {
Log.d("TestTag","exception : "+e);
}
}
}
I tried to upload images to server through camera,gallery and filemanager its working properly but when i select pdf file through filemanage i am getting SSL Exception.
Expected output: If i select pdf files through file manager it should upload pdf files properly