2017-08-11 17 views
0

シンプルなシナリオがあります。内部または外部のストレージからファイルを選択し、サービスを使用してサーバーにアップロードする必要があります。 onActivityResult()uriからファイルを取得できませんでした

このように

開くファイル選択

case R.id.action_cv: { 

    Intent intent = new Intent(Intent.ACTION_GET_CONTENT); 
    intent.setAction(Intent.ACTION_OPEN_DOCUMENT); 
    intent.addCategory(Intent.CATEGORY_OPENABLE); 
    intent.setType("*/*"); 
    startActivityForResult(Intent.createChooser(intent, null), Constant.REQUEST_CODE_OPEN); 

    break; 
} 

私は、ファイル

if (requestCode == Constant.REQUEST_CODE_OPEN) { 
    if (resultCode == RESULT_OK && null != data) { 

     Uri fileUri = data.getData(); 

     if (Utils.validateFileType(Utils.getMimeType(UserProfileActivity.this, fileUri))){ 
      Log.e("File", fileUri.toString()); 
      uploadFile(fileUri.toString()); 
     } else { 
      FutureSmileToast.getInstance() 
        .showToast(getApplicationContext(), "File type is not supported", SuperToast.Background.RED); 
     } 

    } 
} 

を得ることが

E/File: content://com.android.externalstorage.documents/document/6158-1118%3Asome_file.txt 

さて問題は

です uploadFile()方法であることを示しています
private void uploadFile(String filePath) { 

    File file = new File(filePath); 

    String fileNameString = System.currentTimeMillis() + file.getName(); 

    // Parsing any Media type file 
    RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file); 
    MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("cvfile", fileNameString, requestBody); 
    RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName()); 

    Call<ServerResponse> call = mRestManager.getApiService().uploadCVFile(fileToUpload, filename); 
    call.enqueue(new Callback<ServerResponse>() { 
     @Override 
     public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) { 

      if (null != response.body()) { 
       serverResponse = response.body(); 
       if (serverResponse.getSuccess()) { 

        //Further proccesing 

       } else { 
        FutureSmileToast.getInstance() 
          .showToast(getApplicationContext(), serverResponse.getMessage(), SuperToast.Background.RED); 

       } 
      } else { 
       // 
      } 
      dialog.dismiss(); 
     } 

     @Override 
     public void onFailure(Call<ServerResponse> call, Throwable t) { 
      t.printStackTrace(); 
      FutureSmileToast.getInstance() 
        .showToast(getApplicationContext(), "Failed to upload CV", SuperToast.Background.RED); 
     } 
    }); 
} // CV upload 

この方法は

08-11 10:46:35.819 27328-27328/pk.futuresmile W/System.err: java.io.FileNotFoundException: content:/com.android.externalstorage.documents/document/6158-1118%3Asome_file.txt: open failed: ENOENT (No such file or directory) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at libcore.io.IoBridge.open(IoBridge.java:496) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at java.io.FileInputStream.<init>(FileInputStream.java:76) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at okio.Okio.source(Okio.java:166) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at okhttp3.RequestBody$3.writeTo(RequestBody.java:117) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at okhttp3.MultipartBody.writeOrCountBytes(MultipartBody.java:171) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at okhttp3.MultipartBody.writeTo(MultipartBody.java:113) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at okhttp3.logging.HttpLoggingInterceptor.intercept(HttpLoggingInterceptor.java:189) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at okhttp3.RealCall$AsyncCall.execute(RealCall.java:129) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at okhttp3.internal.NamedRunnable.run(NamedRunnable.java:32) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err:  at java.lang.Thread.run(Thread.java:818) 
08-11 10:46:35.823 27328-27328/pk.futuresmile W/System.err: Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory) 
08-11 10:46:35.824 27328-27328/pk.futuresmile W/System.err:  at libcore.io.Posix.open(Native Method) 
08-11 10:46:35.824 27328-27328/pk.futuresmile W/System.err:  at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 
08-11 10:46:35.824 27328-27328/pk.futuresmile W/System.err:  at libcore.io.IoBridge.open(IoBridge.java:482) 
08-11 10:46:35.824 27328-27328/pk.futuresmile W/System.err:  ... 14 more 

がどのようにURIからファイルを取得し、例外が発生し、問題を解決することができます私に例外を与えますか?

+0

は、サーバーにアップロードファイルの前にhttps://stackoverflow.com/a/41520090/1548824 – akhilesh0707

+0

これを試してみてくださいURIからファイルパスを取得する必要があります – akhilesh0707

+1

@AkhileshPatilはい!上記のコメントで提案した解決策を試してみましたが、 'java.io.FileNotFoundException:/storage/emulated/0/some_file.txt:オープンに失敗しました:ENOENT(そのようなファイルやディレクトリはありません)' –

答えて

0

ファイルパスを確認しましたか? 、 それが正しいか ?? 、私の場合、ファイルパスをに変更しましたのようにstorage/file.txt、私は直接パスを書いてテストし、私のために働いていました。今どきのパスからファイルを検索する際に、いくつかの問題..

+0

パスはURIから来る –

+0

それを変更する方法がわからない! –

+0

storage/file.txtのようなストレージ内にファイルを直接カットアンドペーストしてから、fileUploadメソッドでパスをハードコーディングします(File file = new File( "storage/file.txt"); 、私は最初に動作するかどうかを確認するテストのためにこれを行いました.... –

0

は、あなたが以下のようにfileProviderを使用してみました:

Uri uriName = FileProvider.getUriForFile(this, 
         "your.app.fileProviderPath", 
         fileName); 
+0

'your.app.fileProviderPath'をどのように設定するか取得できますか? –

+0

実際には、このケースの 'case R.id.action_cv'から' Intent intent = new Intent(Intent.ACTION_GET_CONTENT) 'を通して' onActivityResult'を呼び出します。私は正しいですか? – sam

+0

はい私はそこにそれを見る!それで?? –

関連する問題