【问题标题】:Looking for a method to convert to bitmap an image with MultipartUploadRequest寻找一种使用 MultipartUploadRequest 将图像转换为位图的方法
【发布时间】:2019-09-29 00:43:38
【问题描述】:

作为一个编程新手,我认为这个网站很棒!

我正在使用 MultipartUploadRequest 库将图像上传到服务器,但我很难找到一种方法来使用位图参数而不是字符串作为 .addFileToUpload 参数。

预期的参数是:

MultipartUploadRequest(this, uploadId, Constants.UPLOAD_URL)
                    .addFileToUpload( path, "image") 
                    .addParameter("name", name) 
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload(); 

由于我想上传位图文件,我正在寻找一种将参数“路径”更改为“位图”的方法。

有可能吗?

谢谢大家!

【问题讨论】:

  • 它来自“net.gotev.uploadservice.MultipartUploadRequest”库。

标签: android image bitmap upload type-conversion


【解决方案1】:

根据这个库,你应该是你要上传的文件的路径字符串,所以你需要将你的位图保存到一个文件然后上传这个文件。
1. 将位图保存到某个位置的文件中:

String path = Environment.getExternalStorageDirectory();
File file = new File(path + "/image_name.jpg"); 
OutputStream fOut = new FileOutputStream(file);
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush(); // Not really required
fOut.close(); // do not forget to close the stream


2. 上传你的文件:

MultipartUploadRequest(this, uploadId, Constants.UPLOAD_URL)
                    .addFileToUpload( path+"/image_name.jpg", "image") 
                    .addParameter("name", name) 
                    .setNotificationConfig(new UploadNotificationConfig())
                    .setMaxRetries(2)
                    .startUpload();


注意:不要忘记授予用户写入和读取外部存储的权限。

【讨论】:

    猜你喜欢
    • 2012-06-21
    • 2016-06-09
    • 2019-09-29
    • 2016-05-11
    • 2017-08-30
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多