2015-11-18 95 views
0

我想將圖片上傳到服務器,但我沒有成功。
我的錯誤在哪裏?我如何上傳圖片

如何發送圖像屬性?

位圖? byte []數組?圖像路徑?
我不知道

這是我的代碼。
請幫幫我。

  try 

     { 
      Uri uri=data.getData(); 
      String[]projection={MediaStore.Images.Media.DATA}; 
      Cursor cursor=getContentResolver().query(uri, projection, null,null,null); 
      cursor.moveToFirst(); 
      String imagepath=cursor.getString(cursor.getColumnIndex(projection[0])); 

      cursor.close(); 

        Bitmap yourSelectedImage=BitmapHelper.getBitmapFromPath(imagepath); 

       Bitmap bm = BitmapFactory.decodeFile(imagepath); 

       BitmapFactory.Options options = null; 
        options = new BitmapFactory.Options(); 
        options.inSampleSize = 3; 
        bm = BitmapFactory.decodeFile(imagepath,options); 
        ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

        bm.compress(Bitmap.CompressFormat.PNG, 50, stream); 
        byte[] byte_arr = stream.toByteArray(); 

     final  String encodedString = Base64.encodeToString(byte_arr, 0); 

       File f = new File(imagepath); 


         imageName = f.getName(); 

         stringToConvert = yourSelectedImage.toString(); 
         theByteArray = stringToConvert.getBytes(); 

          convertimage=BitmapHelper.getByteStringFromBitmap(yourSelectedImage); 


         new ImageGalleryBigPopup(ImageGalleryActivity.this, yourSelectedImage) 
          { 
           @SuppressLint("NewApi") 
           protected void OnSave() throws JSONException { 
            jArr = new JSONArray(); 

             jsonObject.put("FileName", imageName); 
             jsonObject.put("FileContent", encodedString); 
             jsonObject.put("UploadStatus", upstat); 

            jArr.put(jsonObject); 


            for(int i = 0; i < jArr.length(); i++) 
            { 
             //images.add(new ImageGalleryItem(jArr.getJSONObject(i))); 
             saveImage(jArr.getJSONObject(i)); 
            } 

           }; 
          }; 

     } 
      catch(Exception e) 
      { 

       Log.d("GALLERY", e.getMessage()); 
      } 

     } 

這是我saveImage()函數

private void saveImage(JSONObject jsonObject2) throws JSONException 
{ 
    ImageGalleryItem galleryItem=new ImageGalleryItem(jsonObject2); 


    operationType = "0"; 
    operationId = "45"; 
    showLoadingPopup(); 
    //fileName = Converter.encodeUrl(fileName); 
    String url = String.format(UrlConfig.IMAGE_SAVE, operationType, operationId); 
    new ConnectionHelper(ImageGalleryActivity.this,url,galleryItem.toString()) { 

     @Override 
     protected void OnSuccess(String response) { 
      // TODO Auto-generated method stub 
     hideLoadingPopup(); 
      Log.e("ON succes", response); 




      //Toast.makeText(getApplicationContext(),"Resim Eklenmistir", Toast.LENGTH_LONG).show(); 

      getList(); 
     } 

     @Override 
     protected void OnFail(Throwable error, String message) { 
      // TODO Auto-generated method stub 
      hideLoadingPopup(); 
      Log.e("Throwable", error.toString()); 
      Log.e("message", message); 

     } 

     @Override 
     protected void OnException(Exception exception) { 
      // TODO Auto-generated method stub 
      hideLoadingPopup(); 
      Log.e("Exception", exception.toString()); 

     } 
    }; 

} 

回答

-1

它更簡單,通過使用像Ion by Koush

庫您可以使用這種方式:

Ion.with(getBaseContext()) 
    .load("http://your_destination_url") 
    .uploadProgressHandler(new ProgressCallback() { 
     @Override 
     public void onProgress(long uploaded, long total) { 
      // Your progress behaviour 
     } 
    }) 
    .setMultipartFile("name_file", "image/jpeg", new File(YourFile)) 
    .asJsonObject() 
    .setCallback(new FutureCallback<JsonObject>() { 
     @Override 
     public void onCompleted(Exception e, JsonObject result) { 
      // To do on complete upload 
     } 
    }); 
+0

請發佈downvote的原因。 – Darpan

+0

是的,謝謝@Darpan –