Java FileNotFoundException打开失败:在三星设备上载任何类型的文件时Enoint(没有此类文件或目录)

Java FileNotFoundException打开失败:在三星设备上载任何类型的文件时Enoint(没有此类文件或目录),java,android,file-upload,samsung-mobile,Java,Android,File Upload,Samsung Mobile,我想通过从文件管理器中选择文件将文件上载到服务器,因此我已使用此代码单击按钮打开文件管理器 button_upload_attachment.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String[] galleryPermissions = {android.Manifest.permission.REA

我想通过从文件管理器中选择文件将文件上载到服务器,因此我已使用此代码单击按钮打开文件管理器

 button_upload_attachment.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String[] galleryPermissions = {android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE};

            if (EasyPermissions.hasPermissions(CIBILCaptureandUPLOAD.this, galleryPermissions)) {

                Intent intent = new Intent();
                intent.setType("*/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                startActivityForResult(intent, 1);
        } else {
                EasyPermissions.requestPermissions(this, "Access for storage",
                        101, galleryPermissions);
            }
        }
    });
在activityresult方法中,我做了类似的事情来获取文件的路径,并使UploadToserver函数用于上传

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    //ImagesData = data;
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {

            Uri contentUri=data.getData();
            Log.e("bbbbbbbbbbbbbb", contentUri.toString());

            if(contentUri.toString().endsWith(".png")||contentUri.toString().endsWith("jpg") ||
                    contentUri.toString().endsWith(".pdf")){

                photoFile= new File(contentUri.getPath());

                if (photoFile.exists()) {

                    Log.e("photoFile", "File Exists");
                }
                if (photoFile != null) {
                    new AsyncTask<String, String, File>() {
                        ProgressDialog pd;

                        @Override
                        protected void onPreExecute() {
                            pd = ProgressDialog.show(CIBILCaptureandUPLOAD.this, "", "Compressing...");
                            Log.e("PreExecute", "Compressing");
                        }

                        @Override
                        protected File doInBackground(String[] params) {
                            return photoFile;
                        }

                        @Override
                        protected void onPostExecute(File result) {
                            pd.dismiss();
                            if (result != null) {
                                new CIBILCaptureandUPLOAD.UploadFileToServer().execute(result);
                            }
                        }
                    }.execute("");
                }

            }else {
                String[] filePathColumn = {MediaStore.Images.Media.DATA};
                Cursor cursor = getContentResolver().query(contentUri,
                        filePathColumn, null, null, null);
                int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                String filePath = "";
                if (cursor.moveToFirst()) {
                    filePath = cursor.getString(columnIndex);
                }
                Log.e("PATH", filePath);

                photoFile = new File(filePath);

                if (photoFile.exists()) {

                    Log.e("photoFile", "File Exists");
                }
                if (photoFile != null) {
                    new AsyncTask<String, String, File>() {
                        ProgressDialog pd;

                        @Override
                        protected void onPreExecute() {
                            pd = ProgressDialog.show(CIBILCaptureandUPLOAD.this, "", "Compressing...");
                            Log.e("PreExecute", "Compressing");
                        }

                        @Override
                        protected File doInBackground(String[] params) {
                            return photoFile;
                        }

                        @Override
                        protected void onPostExecute(File result) {
                            pd.dismiss();
                            if (result != null) {
                                new CIBILCaptureandUPLOAD.UploadFileToServer().execute(result);
                            }
                        }
                    }.execute("");
                }
            }
        }
我的上传代码是

私有类UploadFileToServer扩展异步任务{ 私有静态最终字符串TAG=“UploadFileToServer”

查看
contentUri.getPath()
的值,确定它不是有效的文件系统路径

相反,您应该为获得的uri打开一个InputStream,并从流中读取文件内容

InputStream is = getContentResolver().openInputStream(contentUri);

你有一个if-else语句,但没有告诉问题是在if还是else块中。你应该只发布该块,而不让我们猜测。我可以在哪里做这件事?然后如何使用InputStream的这个“is”对象你应该使用它来上传你的图像。但是你没有发布图像上传代码…
entity.addPart(“图像”),new FileBody(sourceFile));
。看起来你好像运气不好。这将是你必须更改的唯一语句。但恐怕它无法接受输入流。
    // private ProgressBar progressBar;
    // private String filePath = null;
    // private TextView txtPercentage;

    private ProgressDialog pd;
    long totalSize = 0;

    @Override
    protected void onPreExecute() {
        // setting progress bar to zero
        // progressBar.setProgress(0);
        pd = ProgressDialog.show(CIBILCaptureandUPLOAD.this, "", "Loading...");
        super.onPreExecute();
    }

    @Override
    protected void onProgressUpdate(Integer... progress) {
        // Making progress bar visible
        // progressBar.setVisibility(View.VISIBLE);
        // updating progress bar value
        pd.setMessage("Loading..." + progress[0]);
        // progressBar.setProgress(progress[0]);
        // updating percentage value
        // txtPercentage.setText(String.valueOf(progress[0]) + "%");
    }

    @Override
    protected String doInBackground(File... params) {
        return uploadFile1(params[0]);
    }

    @SuppressWarnings("deprecation")
    private String uploadFile1(File file) {
        String responseString = null;
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(AppUtill.URL_CIBIL_imageupload);
        try {
            AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
                    new AndroidMultiPartEntity.ProgressListener() {

                        @Override
                        public void transferred(long num) {
                            publishProgress((int) ((num / (float) totalSize) * 100));
                        }
                    });
            File sourceFile = file;// new File(filePath);
            entity.addPart("image", new FileBody(sourceFile));
            entity.addPart("mid", new StringBody(memberid));
            entity.addPart("did", new StringBody("0"));
            entity.addPart("fid", new StringBody(formtypeid));

            if (app_loadid == null) {
                SharedPreferences sharedPreferences = getSharedPreferences("LID", MODE_PRIVATE);
                app_loadid = sharedPreferences.getString("lid", "");
                entity.addPart("lid", new StringBody(app_loadid));
            } else {
                entity.addPart("lid", new StringBody(app_loadid));
            }

            totalSize = entity.getContentLength();
            httppost.setEntity(entity);
            // Making server call
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity r_entity = response.getEntity();
            int statusCode = response.getStatusLine().getStatusCode();
            System.out.println("statusCode +statusCode");
            if (statusCode == 200) {
                // Server response
                System.out.println("statusCode +statusCode");
                responseString = EntityUtils.toString(r_entity);
                String success = String.valueOf(responseString.contains("1"));

                if (success.matches("true")) {
                    uploadedFileCount++;
                } else {
                    runOnUiThread(new Runnable() {
                        public void run() {
                            Toast.makeText(getApplicationContext(), "File not uploaded. Please upload again...", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            } else {
                responseString = "Error occurred! Http Status Code: " + statusCode;
            }
        } catch (ClientProtocolException e) {
            responseString = e.toString();
        } catch (IOException e) {
            responseString = e.toString();
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String result) {
        Log.e(TAG, "Response from server: " + result);
        pd.dismiss();

        if (uploadedFileCount == MAX_COUNT) {
            AlertDialog.Builder alert = new AlertDialog.Builder(CIBILCaptureandUPLOAD.this);
            alert.setTitle(R.string.app_name);
            alert.setMessage("Reach to max upload limit");
            alert.setCancelable(false);
            alert.setPositiveButton("OK", null);
            alert.create().show();
        }
        // showing the server response in an alert dialog
        showAlert(result);
        AppUtill.deleteFolderAndAllFile(CIBILCaptureandUPLOAD.this);

        super.onPostExecute(result);
    }
}
photoFile= new File(contentUri.getPath());
InputStream is = getContentResolver().openInputStream(contentUri);