我想通过从文件管理器中选择文件来将文件上传到服务器,所以我通过使用此代码单击“按钮”来打开文件管理器,
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); } } });
和onActivityResult方法,我已经做了类似的事情来获取文件的路径,并让UploadToserver函数可以上传
@OverrIDepublic 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(""); } } }
它在其他设备上效果很好,但是在三星设备上
java.io.fileNotFoundException: /document/primary:Xender/other/When Strangers Meet_ 3 in 1 Box - John Harker.pdf: open Failed: ENOENT (No such file or directory)
我的上传代码是
私有类UploadfileToServer扩展了AsyncTask {
私有静态最终字符串TAG =“ UploadfileToServer”;
// 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());
查看contentUri.getPath()的值,并确认它不是有效的文件系统路径.
相反,您应该为获取的uri打开inputStream并从流中读取文件内容.
inputStream is = getContentResolver().openinputStream(contentUri);
总结 以上是内存溢出为你收集整理的java-FileNotFoundException打开失败:Samsung设备上载任何类型的文件时,ENOENT(无此类文件或目录)全部内容,希望文章能够帮你解决java-FileNotFoundException打开失败:Samsung设备上载任何类型的文件时,ENOENT(无此类文件或目录)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)