【问题标题】:Upload image from android gallery to Firebase storage using HttpPost使用 HttpPost 将图像从 android 图库上传到 Firebase 存储
【发布时间】:2017-10-26 23:56:13
【问题描述】:

我创建了一个将图片从画廊上传到 Firebase 存储的 android 应用程序,我需要使用 HttpPost 来上传图片。

我的代码如下:

@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();

在 doInBackground 方法中我调用 postAvatarToServer 如下:

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;

        }

在 logcat 中我得到!发生错误! Http状态码:400

那么错误在哪里以及如何解决它......

【问题讨论】:

    标签: android upload http-post firebase-storage


    【解决方案1】:

    为什么不使用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);
    

    【讨论】:

    • 是的,我知道我可以使用它。我尝试了它和它的工作,但我必须使用 HttpPost 进行上传。我的代码是为了证明使用 HttpPost 可以将图像上传到 Firebase
    猜你喜欢
    • 1970-01-01
    • 2019-02-11
    • 2018-06-05
    • 2017-09-25
    • 2018-07-04
    • 1970-01-01
    相关资源
    最近更新 更多