0

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Vishali
  • 1,307
  • 4
  • 23
  • 41
  • paste logcat for more explanation – Pradip Vadher Jul 04 '19 at 12:59
  • 1
    That `getPath()` method will not work on all devices today and will fail for every Android Q+ device. Get rid of that method and use the `Uri` properly. See https://stackoverflow.com/a/54843979/115145 and https://stackoverflow.com/q/49221312/115145 and https://stackoverflow.com/q/48510584/115145 and https://stackoverflow.com/q/35870825/115145. – CommonsWare Jul 04 '19 at 13:12
  • Will fail if your app targets Android Q and will work otherwise – Anton Malyshev Jul 04 '19 at 13:41
  • in somecases the pdf files are uploading perfectly.But i don't know why its not working all the times – Vishali Jul 05 '19 at 03:51

0 Answers0