Android 如何使用改型将所需大小的图像以多部分格式按规格最大大小50 MB上传到服务器?

Android 如何使用改型将所需大小的图像以多部分格式按规格最大大小50 MB上传到服务器?,android,Android,我正在从画廊或相机上传图片,我是android新手,请帮助。使用改型将图像作为多部分发送。我需要将图像作为 高度:4.5厘米(640像素) 宽度:3.5厘米(480像素) 图像的完整大小不应超过50 MB。您可以在发送之前检查文件大小: File file = new File("/sdcard/image.jpg"); long length = file.length(); length = length/1024; if(length < 50000) { upload(f

我正在从画廊或相机上传图片,我是android新手,请帮助。使用改型将图像作为多部分发送。我需要将图像作为

高度:4.5厘米(640像素) 宽度:3.5厘米(480像素)


图像的完整大小不应超过50 MB。

您可以在发送之前检查文件大小:

File file = new File("/sdcard/image.jpg");
long length = file.length();
length = length/1024;

if(length < 50000) {
    upload(file);
} else {
    reduceSize(file);
}

.
.
.

public static boolean reduceSize(File img, long maxSize) {
    boolean result = false;
    BitmapFactory.Options options = new BitmapFactory.Options();
    Bitmap bitmap = null;
    options.inSampleSize = 1;
    while (img.length() > maxSize) {
        img = new File(path);
        options.inSampleSize = options.inSampleSize+1;
        bitmap = BitmapFactory.decodeFile(path, options);
        Utils.deleteRecursive(path);
        result = saveImage(bitmap, path);
    };
    return result;
}

private void upload(file) {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

    // Change base URL to your upload server URL.
    service = new Retrofit.Builder().baseUrl("http://192.168.0.234").client(client).build().create(Service.class);

    RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile);
    RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "upload_test");

    retrofit2.Call<okhttp3.ResponseBody> req = service.postImage(body, name);
    req.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
            // Do Something
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            t.printStackTrace();
        }
    });
}
File File=new文件(“/sdcard/image.jpg”);
long length=file.length();
长度=长度/1024;
如果(长度<50000){
上传(文件);
}否则{
简化(文件);
}
.
.
.
公共静态布尔约简(文件img,long maxSize){
布尔结果=假;
BitmapFactory.Options=new-BitmapFactory.Options();
位图=空;
options.inSampleSize=1;
while(img.length()>maxSize){
img=新文件(路径);
options.inSampleSize=options.inSampleSize+1;
位图=BitmapFactory.decodeFile(路径、选项);
Utils.deleteRecursive(路径);
结果=保存图像(位图、路径);
};
返回结果;
}
私有无效上载(文件){
HttpLoggingInterceptor拦截器=新的HttpLoggingInterceptor();
拦截器.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient客户端=新建OkHttpClient.Builder().addInterceptor(拦截器).build();
//将基本URL更改为上载服务器URL。
服务=新改装.Builder().baseUrl(“http://192.168.0.234).client(client.build().create(Service.class);
RequestBody reqFile=RequestBody.create(MediaType.parse(“image/*”),文件);
MultipartBody.Part body=MultipartBody.Part.createFormData(“upload”,file.getName(),reqFile);
RequestBody name=RequestBody.create(MediaType.parse(“text/plain”),“upload\u test”);
2.Call req=service.positmage(body,name);
请求排队(新回调(){
@凌驾
public void onResponse(调用调用,响应响应){
//做点什么
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
t、 printStackTrace();
}
});
}

您可以在发送文件之前检查文件大小:

File file = new File("/sdcard/image.jpg");
long length = file.length();
length = length/1024;

if(length < 50000) {
    upload(file);
} else {
    reduceSize(file);
}

.
.
.

public static boolean reduceSize(File img, long maxSize) {
    boolean result = false;
    BitmapFactory.Options options = new BitmapFactory.Options();
    Bitmap bitmap = null;
    options.inSampleSize = 1;
    while (img.length() > maxSize) {
        img = new File(path);
        options.inSampleSize = options.inSampleSize+1;
        bitmap = BitmapFactory.decodeFile(path, options);
        Utils.deleteRecursive(path);
        result = saveImage(bitmap, path);
    };
    return result;
}

private void upload(file) {
    HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
    interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
    OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();

    // Change base URL to your upload server URL.
    service = new Retrofit.Builder().baseUrl("http://192.168.0.234").client(client).build().create(Service.class);

    RequestBody reqFile = RequestBody.create(MediaType.parse("image/*"), file);
    MultipartBody.Part body = MultipartBody.Part.createFormData("upload", file.getName(), reqFile);
    RequestBody name = RequestBody.create(MediaType.parse("text/plain"), "upload_test");

    retrofit2.Call<okhttp3.ResponseBody> req = service.postImage(body, name);
    req.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) { 
            // Do Something
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            t.printStackTrace();
        }
    });
}
File File=new文件(“/sdcard/image.jpg”);
long length=file.length();
长度=长度/1024;
如果(长度<50000){
上传(文件);
}否则{
简化(文件);
}
.
.
.
公共静态布尔约简(文件img,long maxSize){
布尔结果=假;
BitmapFactory.Options=new-BitmapFactory.Options();
位图=空;
options.inSampleSize=1;
while(img.length()>maxSize){
img=新文件(路径);
options.inSampleSize=options.inSampleSize+1;
位图=BitmapFactory.decodeFile(路径、选项);
Utils.deleteRecursive(路径);
结果=保存图像(位图、路径);
};
返回结果;
}
私有无效上载(文件){
HttpLoggingInterceptor拦截器=新的HttpLoggingInterceptor();
拦截器.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient客户端=新建OkHttpClient.Builder().addInterceptor(拦截器).build();
//将基本URL更改为上载服务器URL。
服务=新改装.Builder().baseUrl(“http://192.168.0.234).client(client.build().create(Service.class);
RequestBody reqFile=RequestBody.create(MediaType.parse(“image/*”),文件);
MultipartBody.Part body=MultipartBody.Part.createFormData(“upload”,file.getName(),reqFile);
RequestBody name=RequestBody.create(MediaType.parse(“text/plain”),“upload\u test”);
2.Call req=service.positmage(body,name);
请求排队(新回调(){
@凌驾
public void onResponse(调用调用,响应响应){
//做点什么
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
t、 printStackTrace();
}
});
}
类图像文件(val-uri:uri,name:String){
val文件名:字符串
初始化{
val file=file(Environment.getExternalStorageDirectory().toString()+“/Documents”)
如果(!file.exists()){
file.mkdirs()文件
}
val fileNoMedia=File(File.absolutePath+“/.nomedia”)
如果(!fileNoMedia.exists())
fileNoMedia.createNewFile()文件
if(name.toLowerCase().endsWith(“.pdf”)){
filename=file.absolutePath+“/”+System.currentTimeMillis()+“.pdf”
}否则{
filename=file.absolutePath+“/”+System.currentTimeMillis()+“.jpg”
}
}
@抛出(IOException::类)
有趣的copyFileStream(上下文:上下文,uri:uri):字符串{
if(filename.endsWith(“.pdf”)| | filename.endsWith(“.pdf”)){
变量ins:InputStream?=null
var os:OutputStream?=null
试一试{
ins=context.getContentResolver().openInputStream(uri)
os=FileOutputStream(文件名)
val缓冲区=字节数组(1024)
变量长度:Int=ins.read(缓冲区)
而(长度>0){
写操作(缓冲区,0,长度);
长度=ins.read(缓冲区)
}
}捕获(e:例外){
e、 printStackTrace();
}最后{
ins?.close()
os?.close()
}
}否则{
变量ins:InputStream?=null
var os:OutputStream?=null
试一试{
ins=context.getContentResolver().openInputStream(uri)
var scaledBitmap:位图?=null
val options=BitmapFactory.options()
options.inJustDecodeBounds=true
var bmp=BitmapFactory.decodeStream(ins、null、选项)
var actualHeight=options.outHeight
var actualWidth=options.outWidth
//压缩图像的最大高度和宽度值为816x612
val maxHeight=816.0华氏度