亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將圖像上傳到服務器android

將圖像上傳到服務器android

HUWWW 2019-10-06 13:13:36
我想知道哪種是將圖像上傳到服務器而不降低其質量的最佳方法。我在Google上搜索時發現了各種發布數據的方法。但是我不確定哪一個最好上傳。我碰到多部分圖像上傳。使用字節數組上傳圖像使用base64編碼的字符串上傳圖像。我嘗試過Base64編碼,如果圖像的分辨率太高,它將導致OOM(內存不足)。任何解決這個問題的教程將不勝感激。提前致謝。
查看完整描述

3 回答

?
墨色風雨

TA貢獻1853條經驗 獲得超6個贊

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);

photoPickerIntent.setType("image/*");

startActivityForResult(photoPickerIntent, 1);

上面的代碼從圖庫中選擇圖像


@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1)

        if (resultCode == Activity.RESULT_OK) {

            Uri selectedImage = data.getData();


            String filePath = getPath(selectedImage);

            String file_extn = filePath.substring(filePath.lastIndexOf(".") + 1);

            image_name_tv.setText(filePath);


            try {

                if (file_extn.equals("img") || file_extn.equals("jpg") || file_extn.equals("jpeg") || file_extn.equals("gif") || file_extn.equals("png")) {

                    //FINE

                } else {

                    //NOT IN REQUIRED FORMAT

                }

            } catch (FileNotFoundException e) {

                // TODO Auto-generated catch block

                e.printStackTrace();

            }

        }

}


public String getPath(Uri uri) {

    String[] projection = {MediaColumns.DATA};

    Cursor cursor = managedQuery(uri, projection, null, null, null);

    column_index = cursor

            .getColumnIndexOrThrow(MediaColumns.DATA);

    cursor.moveToFirst();

    imagePath = cursor.getString(column_index);


    return cursor.getString(column_index);

}

現在使用多方表格數據過帳數據


HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("LINK TO SERVER");

多部分表格數據


MultipartEntity mpEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

if (filePath != null) {

    File file = new File(filePath);

    Log.d("EDIT USER PROFILE", "UPLOAD: file length = " + file.length());

    Log.d("EDIT USER PROFILE", "UPLOAD: file exist = " + file.exists());

    mpEntity.addPart("avatar", new FileBody(file, "application/octet"));

}

最后將數據發布到服務器


httppost.setEntity(mpEntity);

HttpResponse response = httpclient.execute(httppost);


查看完整回答
反對 回復 2019-10-06
  • 3 回答
  • 0 關注
  • 565 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號