2017-10-26 2 views
0

갤러리에서 Firebase 스토리지로 이미지를 업로드하는 안드로이드 응용 프로그램을 만듭니다. HttpPost를 사용하여 이미지를 업로드해야합니다.HttpPost를 사용하여 안드로이드 갤러리에서 Firebase 스토리지로 이미지 업로드

내 코드는 다음과 같습니다 다음과 같습니다 내가 postAvatarToServer를 호출 doInBackground 방법에

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent 
data) { 
switch (requestCode) { 
case TAKE_AVATAR_GALLERY_REQUEST: 
if (resultCode == Activity.RESULT_OK) { 
Uri photoUri = data.getData(); 
Bitmap avatar= Media.getBitmap(getContentResolver(), photoUri); 
String strAvatarFilename = "avatar.jpg"; 
try { 
avatar.compress(CompressFormat.JPEG, 100, 
openFileOutput(strAvatarFilename, MODE_PRIVATE)); 
} catch (Exception e) { 
Log.e(DEBUG_TAG, "Avatar compression and save failed.", e); 
} 

Uri imageUriToSaveCameraImageTo = Uri.fromFile(new File(
QuizSettingsActivity.this.getFilesDir(), strAvatarFilename)); 
Editor editor = mGameSettings.edit(); 
editor.putString(GAME_PREFERENCES_AVATAR, 
imageUriToSaveCameraImageTo.getPath()); 
editor.commit(); 

!

로그 캣에서
private boolean postAvatarToServer() { 
    boolean succeeded = false; 
    String avatar = mGameSettings.getString(GAME_PREFERENCES_AVATAR, ""); 
    MultipartEntity entity = new 
      MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
    File file = new File(avatar); 
    if (file.exists()) { 
     FileBody encFile = new FileBody(file); 
     entity.addPart("avatar", encFile); 
     HttpPost request = new 
      HttpPost("https://firebasestorage.googleapis.com/v0/b/HERE I 
      WRITE THE FOLDER URL WITHOUT [gs://]/o/"); 
     request.setEntity(entity); 
     HttpClient client = new DefaultHttpClient(); 
     try { 
      HttpResponse response = client.execute(request); 
      HttpEntity r_entity = response.getEntity(); 
      int statusCode = response.getStatusLine().getStatusCode(); 
      String responseString = null; 
      if (statusCode == 200) { 
        responseString = EntityUtils.toString(r_entity); 
      } else { 
        responseString = "Error occurred! Http Status Code: " 
           + statusCode; 
      } 
      succeeded = true; 

     } catch (ClientProtocolException e) { 
      Log.e(DEBUG_TAG, "Unexpected ClientProtocolException",e); 
     } catch (IOException e) { 
      Log.e(DEBUG_TAG, "Unexpected IOException", e); 
     } 
} else { 
    Log.d(DEBUG_TAG, "No avatar to upload"); 
    succeeded = true; 
      } 
    return succeeded; 

     } 

내가 얻을 오류가 발생했습니다! HTTP 상태 코드 : 400

어디 오류가 그것을 해결하는 방법을 ...

답변

0

the existing Android SDK를 사용하지 않습니까?

Uri file = Uri.fromFile(new File("path/to/local/file.jpg")); 
StorageReference storageRef = storageRef.child("images/"+file.getLastPathSegment()); 
uploadTask = storageRef.putFile(file); 
+0

예, 사용할 수 있음을 알고 있습니다. 나는 그것과 그 일을 시도하지만 나는 HttpPost를 사용하여 업로드를해야만한다. 내 코드는 HttpPost를 사용하여 Firebase에 이미지를 업로드 할 수 있음을 증명합니다. – user216905

관련 문제