2015-07-28 37 views
0

我是新的改造,並在我的應用程序中,我試圖發送圖像到服務器從圖庫使用retrofit,但我一直在收到「RetrofitError:預期的329字節,但收到4096「的改造失敗方法的迴應。我從onAvtivityResult的intent中創建位圖,並將文件對象的路徑作爲名稱和位圖。RetrofitError:預計329字節,但在Android中收到4096

Uri mImgUri = returnedIntent.getData(); 
       InputStream imageStream = getContentResolver().openInputStream(mImgUri); 
       final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); 

        This is the error log, 

        retrofit.RetrofitError: expected 329 bytes but received 4096 
           at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:395) 
           at retrofit.RestAdapter$RestHandler.access$100(RestAdapter.java:220) 
           at retrofit.RestAdapter$RestHandler$2.obtainResponse(RestAdapter.java:278) 
           at retrofit.CallbackRunnable.run(CallbackRunnable.java:42) 
           at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 
           at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 
           at retrofit.Platform$Android$2$1.run(Platform.java:142) 
           at java.lang.Thread.run(Thread.java:856) 
         Caused by: java.net.ProtocolException: expected 329 bytes but received 4096 
           at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:299) 
           at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:133) 
           at okio.RealBufferedSink$1.write(RealBufferedSink.java:148) 
           at retrofit.mime.TypedFile.writeTo(TypedFile.java:78) 

       This is my Retrofit adapter, 

       private void startRetrofitTask(Bitmap mBitmap,Uri mImgUri){ 

         String mimeType = "image/png"; 
         File mFile = AppUtils.convertBitmapToFile(this,mImgUri.getLastPathSegment(),mBitmap); 
         TypedFile fileToSend = null; 
         RestAdapter restAdapter = new RestAdapter.Builder() 
           .setEndpoint(Constants.LOCAL_BASE_URL) 
           .setClient(new OkClient(new OkHttpClient())) 
           .build(); 
         RetrofitCommonService mRetrofitCommonService = restAdapter.create(RetrofitCommonService.class); 

         if(mFile != null){ 
          fileToSend = new TypedFile(mimeType, mFile); 
         } 
         mRetrofitCommonService.upLoadImage(new AppUtils(this).getPhone(), 
           getDeviceId(), fileToSend,this); 
        } 


      public static File convertBitmapToFile(Context mContext,String mName,Bitmap bitmap) { 
        if(bitmap == null)return null; 
        File filesDir = mContext.getFilesDir(); 
        File imageFile = new File(filesDir, mName + ".png"); 
        OutputStream os; 
        try { 
         os = new FileOutputStream(imageFile); 
         bitmap.compress(Bitmap.CompressFormat.PNG, 100, os); 
         os.flush(); 
         os.close(); 
        } catch (Exception e) { 
         Log.e("TAG", "Error writing bitmap", e); 
        } 
        return imageFile; 
       } 


     @Multipart 
      @POST("/user/asset") 
      @Headers("Accept:application/json") 
      void upLoadImage(@Header("mobile-number") String mPhone, @Header("uid") String imei, 
            @Part("file") TypedFile mBody,Callback<RetrofitResponse> response); 
+0

我得到了同樣的錯誤在一個手機,其他的手機都OK。我不知道什麼是錯的。 –

回答

0

我不知道你爲什麼要將位圖uri轉換成文件。從URI獲取照片URL字符串,並像這樣發佈。而已。

這對我很好!

適配器設置&後:

restPhotoAdapter = new RestAdapter.Builder() 
      .setClient(new OkClient(okHttpClient)) 
      .setEndpoint(Config.BASE_API_PHOTO_URL) 
      .setLogLevel((BuildConfig.DEBUG) ? RestAdapter.LogLevel.FULL : RestAdapter.LogLevel.NONE) 
      .build(); 


public void postPhoto(final String url, 
           final ControllerCallback<List<Photo>> postRequestCallback) { 
    TypedFile file = new TypedFile("image/jpeg", new File(url)); 
    Repository repository = restPhotoAdapter.create(Repository.class); 
    repository.postPhoto(file, new Callback<List<Photo>>() { 
     @Override 
     public void success(List<Photo> photoOut, Response response) { 
      postRequestCallback.success(photoOut); 
     } 

     @Override 
     public void failure(RetrofitError error) { 
      postRequestCallback.failure(new CustomError(error)); 
     } 
    }); 
} 
+0

這個答案給出了另一種方法,但不回答問題 – kike

0
 @chain thanks for the response and it worked for me, 
and i have done some change in my code for making file from Uri and it really worked. 


    TypedFile fileToSend = new TypedFile("image/png", new File(AppUtils.getRealPathFromURI(context, mImgUri))); 


     public static String getRealPathFromURI(Context context,Uri contentURI) { 
       String result; 
       Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null); 
       if (cursor == null) { // Source is Dropbox or other similar local file path 
        result = contentURI.getPath(); 
       } else { 
        cursor.moveToFirst(); 
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
        result = cursor.getString(idx); 
        cursor.close(); 
       } 
       return result; 
      } 
+0

太棒了!不用謝 !!! – chain

相關問題