为了账号安全,请及时绑定邮箱和手机立即绑定

将图像上传到服务器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 关注
  • 536 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信