2016-08-09 12 views
0

hereの指示に従ってRetrofit POSTを試しています。この記事では、次のように非常に古いファイルピッカーを使用することを推奨しています。open failed:ENOENT(そのようなファイルまたはディレクトリはありません)正しいURI形式を取得できません

//https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java 
// use the FileUtils to get the actual file by uri 
File file = FileUtils.getFile(this, fileUri); 

代わりに、以下のコードを使用してファイルを取得するためにインテントを使用することにしました。

uploadButton.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     Intent intent = new Intent(); 
     intent.setType("image/*"); 
     intent.setAction(Intent.ACTION_GET_CONTENT); 
     startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST); 
    } 
}); 


@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == PICK_IMAGE_REQUEST && resultCode == Activity.RESULT_OK 
       && data != null && data.getData() != null) { 
      Uri fileUri = data.getData(); 

      Log.d(LOG_TAG, "fileUri " + fileUri.toString()); 

      try { 
       File file = new File(fileUri.getPath()); 
       Log.d(LOG_TAG, "file " + file.toString()); 
       // create RequestBody instance from file 
       RequestBody requestFile = 
         RequestBody.create(MediaType.parse("multipart/form-data"), file.getAbsoluteFile()); 

       // MultipartBody.Part is used to send also the actual file name 
       MultipartBody.Part body = 
         MultipartBody.Part.createFormData("picture", file.getName(), requestFile); 

       // add another part within the multipart request 
       String descriptionString = "POINT(12.9085608 77.6106535)"; 

       RequestBody location = 
         RequestBody.create(
           MediaType.parse("multipart/form-data"), descriptionString); 

       // finally, execute the request 
       Call<ResponseBody> call = mAbService.uploadImage(location, body); 
       call.enqueue(new Callback<ResponseBody>() { 
        @Override 
        public void onResponse(Call<ResponseBody> call, 
              Response<ResponseBody> response) { 
         Log.v(LOG_TAG, "success"); 
        } 

        @Override 
        public void onFailure(Call<ResponseBody> call, Throwable t) { 
         Log.e(LOG_TAG, t.getMessage()); 
        } 
       }); 
      } catch (Exception e) { 
       Log.d(LOG_TAG, "file failed"); 
      } 
     } 

私のサーバーにファイルをアップロードできるように正しいパスを取得できません。

08-10 01:28:36.564 20093-20093/com.sudhirkhanger.app.test D/TestActivityFragment: fileUri content://com.android.providers.media.documents/document/image%3A57373 
08-10 01:28:36.564 20093-20093/com.sudhirkhanger.app.test D/TestActivityFragment: file /document/image:57373 
08-10 01:28:36.658 20093-20093/com.sudhirkhanger.app.test E/TestActivityFragment: /document/image:57373: open failed: ENOENT (No such file or directory) 

答えて

2

ACTION_GET_CONTENTファイルを返しません。コンテンツを指すUriを返します。そのコンテンツはファイルでなくても、アクセスできるものはもちろんです。 File file = new File(fileUri.getPath());は役に立たない。

私はOkHttp3/RetrofitのRequestBodyを使用していません。私が見ることができるから、create your own implementation that can work off of an InputStreamが必要です。あなたはでgetContentResolver()を呼び出すことによって得られるContentResolveropenInputStream()を呼び出すことによってそれがInputStreamになるでしょう。

+0

'ファイルファイル=新しいファイル("/storage/emulated/0/Pictures/Screenshots/test.png ");'もし私が上記のようにすれば、それはうまくいくはずです。私は、POSTが動作していることを確認したい。今はうまくいかないようです。これが私のRetrofitクラスのファイルまたは実際の問題のためかどうかはわかりません。 https://github.com/sudhirkhanger/AswachhBharatUnofficial –

関連する問題