欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android開(kāi)發(fā)實(shí)現(xiàn)圖片的上傳下載

 更新時(shí)間:2022年09月19日 09:19:18   作者:不會(huì)安卓  
這篇文章主要為大家詳細(xì)介紹了Android開(kāi)發(fā)實(shí)現(xiàn)圖片的上傳下載,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

Android圖片上傳的應(yīng)用場(chǎng)景,供大家參考,具體內(nèi)容如下

在Android開(kāi)發(fā)中,很多時(shí)候我們需要進(jìn)行圖片,文件的上傳下載,最直接的一個(gè)應(yīng)用場(chǎng)景就是用戶頭像的保存與切換,以及像即時(shí)通訊中的圖片發(fā)送等任何在App中設(shè)計(jì)圖片的應(yīng)用都是通過(guò)圖片的上傳下載完成的。

Android 如何實(shí)現(xiàn)圖片的上傳下載?

近期在工作中遇到了需要實(shí)現(xiàn)其圖片上傳的功能,特此寫(xiě)了一個(gè)demo幫助更多的人進(jìn)行相關(guān)學(xué)習(xí)

這是demo的效果圖:

其實(shí)整個(gè)過(guò)程也非常簡(jiǎn)單,圖片上傳可以整理為訪問(wèn)本地選取圖片,以及圖片提交上傳(后端提供上傳圖片接口,這里只對(duì)安卓技術(shù)進(jìn)行相關(guān)說(shuō)明)。

實(shí)現(xiàn)這一過(guò)程這邊使用了PictureSelector+Xutill或者PictureSelector+OkHttp進(jìn)行了功能的實(shí)現(xiàn)。

pictureSelector是一個(gè)強(qiáng)大的Android選取圖片,上傳圖片的開(kāi)源工具,支持從相冊(cè)或拍照選擇圖片或視頻、音頻,支持動(dòng)態(tài)權(quán)限獲取、裁剪(單圖or多圖裁剪)、壓縮、主題自定義配置等功能、適配android 6.0+系統(tǒng),而且你能遇到的問(wèn)題,README文檔都有解決方案。

xutils是前幾年比較火得一個(gè)開(kāi)源框架,主要分四個(gè)重要的模塊:ViewUtils,HttpUtils,BitmapUtils,DbUtils,包含了很多實(shí)用的工具類,支持大文件上傳,且有更全面的http請(qǐng)求協(xié)議支持,擁有靈活的Orm,支持事件注解且不受代碼混淆影響。

xUtils3網(wǎng)絡(luò)模塊大大方便了在實(shí)際開(kāi)發(fā)中網(wǎng)絡(luò)模塊的開(kāi)發(fā),xUtils3網(wǎng)絡(luò)模塊大致包括GET請(qǐng)求、POST請(qǐng)求、請(qǐng)求方式、上傳文件、下載文件、使用緩存等功能

Android 圖片上傳下載實(shí)現(xiàn)過(guò)程(小白教學(xué))

Demo的UI界面:MainActivity的activity_main xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
? ? xmlns:app="http://schemas.android.com/apk/res-auto"
? ? xmlns:tools="http://schemas.android.com/tools"
? ? android:layout_width="match_parent"
? ? android:layout_height="match_parent"
? ? android:background="@color/coreGray"
? ? android:orientation="vertical"
? ? tools:context=".MainActivity">
?
?
? ? <include layout="@layout/layout_title"/>
?
? ? <LinearLayout
? ? ? ? style="@style/LoginLayoutStyle"
?
? ? ? ? android:layout_width="match_parent">
?
? ? ? ? <TextView
? ? ? ? ? ? android:id="@+id/aaa"
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_gravity="center"
? ? ? ? ? ? android:text="AAA:"
? ? ? ? ? ? android:textColor="@color/black60"
? ? ? ? ? ? android:textSize="20dp" />
?
? ? ? ? <com.tz.picturedemo.view.ClearEditText
? ? ? ? ? ? android:id="@+id/qs_username2"
? ? ? ? ? ? style="@style/EditTextStyle"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:hint="請(qǐng)輸入AAAA"
? ? ? ? ? ? android:inputType="text"
? ? ? ? ? ? android:lines="1"
? ? ? ? ? ? android:singleLine="true"
? ? ? ? ? ? android:textCursorDrawable="@null"
? ? ? ? ? ? app:regexType="name" />
? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? style="@style/LoginLayoutStyle"
? ? ? ? android:layout_width="match_parent">
?
? ? ? ? <TextView
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_gravity="center"
? ? ? ? ? ? android:text="BBB:"
? ? ? ? ? ? android:textColor="@color/black60"
? ? ? ? ? ? android:textSize="20dp" />
?
? ? ? ? <com.tz.picturedemo.view.ClearEditText
? ? ? ? ? ? android:id="@+id/qs_username3"
? ? ? ? ? ? style="@style/EditTextStyle"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:hint="請(qǐng)輸入BBBB"
? ? ? ? ? ? android:inputType="text"
? ? ? ? ? ? android:lines="1"
? ? ? ? ? ? android:singleLine="true"
? ? ? ? ? ? android:textCursorDrawable="@null"
? ? ? ? ? ? app:regexType="name" />
? ? </LinearLayout>
? ? <LinearLayout
? ? ? ? style="@style/LoginLayoutStyle"
? ? ? ? android:layout_width="match_parent">
?
? ? ? ? <TextView
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_gravity="center"
? ? ? ? ? ? android:text="CCC:"
? ? ? ? ? ? android:textColor="@color/black60"
? ? ? ? ? ? android:textSize="20dp" />
?
? ? ? ? <com.tz.picturedemo.view.ClearEditText
? ? ? ? ? ? android:id="@+id/qs_username"
? ? ? ? ? ? style="@style/EditTextStyle"
? ? ? ? ? ? android:layout_width="match_parent"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:hint="請(qǐng)輸入CCCC"
? ? ? ? ? ? android:inputType="text"
? ? ? ? ? ? android:lines="1"
? ? ? ? ? ? android:singleLine="true"
? ? ? ? ? ? android:textCursorDrawable="@null"
? ? ? ? ? ? app:regexType="name" />
? ? </LinearLayout>
?
? ? <TextView
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:text="上傳圖片"
? ? ? ? android:textColor="@color/black60"
? ? ? ? android:padding="10dp"
? ? ? ? android:textSize="20sp"/>
? ? <LinearLayout
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="wrap_content"
? ? ? ? android:paddingTop="20dp"
? ? ? ? android:orientation="horizontal">
?
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_weight="1"
? ? ? ? ? ? android:layout_gravity="center"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:orientation="vertical">
? ? ? ? ? ? <ImageView
? ? ? ? ? ? ? ? android:id="@+id/picture1"
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:src="@mipmap/ic_add_image"/>
?
? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:paddingTop="10dp"
? ? ? ? ? ? ? ? android:text="正面"
? ? ? ? ? ? ? ? android:textColor="@color/black60"
? ? ? ? ? ? ? ? android:textSize="18sp" />
?
? ? ? ? </LinearLayout>
?
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_weight="1"
? ? ? ? ? ? android:layout_gravity="center"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:orientation="vertical">
? ? ? ? ? ? <ImageView
? ? ? ? ? ? ? ? android:id="@+id/picture2"
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:src="@mipmap/ic_add_image"/>
?
? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:paddingTop="10dp"
? ? ? ? ? ? ? ? android:text="側(cè)面"
? ? ? ? ? ? ? ? android:textColor="@color/black60"
? ? ? ? ? ? ? ? android:textSize="18sp" />
?
? ? ? ? </LinearLayout>
?
? ? ? ? <LinearLayout
? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? android:layout_weight="1"
? ? ? ? ? ? android:layout_gravity="center"
? ? ? ? ? ? android:gravity="center"
? ? ? ? ? ? android:orientation="vertical">
? ? ? ? ? ? <ImageView
? ? ? ? ? ? ? ? android:id="@+id/picture3"
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:src="@mipmap/ic_add_image"/>
?
? ? ? ? ? ? <TextView
? ? ? ? ? ? ? ? android:layout_width="wrap_content"
? ? ? ? ? ? ? ? android:layout_height="wrap_content"
? ? ? ? ? ? ? ? android:paddingTop="10dp"
? ? ? ? ? ? ? ? android:text="反面"
? ? ? ? ? ? ? ? android:textColor="@color/black60"
? ? ? ? ? ? ? ? android:textSize="18sp" />
? ? ? ? </LinearLayout>
?
? ? </LinearLayout>
?
? ? <Button
? ? ? ? android:id="@+id/makesure"
? ? ? ? android:layout_width="match_parent"
? ? ? ? android:layout_height="45dp"
? ? ? ? android:text="提交"
? ? ? ? android:layout_marginLeft="15dp"
? ? ? ? android:layout_marginRight="15dp"
? ? ? ? android:layout_marginTop="50dp"
? ? ? ? android:textSize="20dp"
? ? ? ? android:textColor="@color/white"
? ? ? ? android:background="@drawable/share_item_blue_bg"
? ? ? ? />
</LinearLayout>

我在此處為了界面美化,引用了一些第三方自定義view大家可自行替換成原生即可

Demo功能核心代碼 MainActivity

package com.tz.picturedemo;
?
import static com.luck.picture.lib.config.SelectMimeType.ofImage;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.luck.picture.lib.basic.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.SelectMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.interfaces.OnResultCallbackListener;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import okhttp3.Call;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
?
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
?
? ? private ImageView mPicture1;
? ? private ImageView mPicture2;
? ? private ImageView mPicture3;
? ? private ?List<String> ?PictureArr=new ArrayList<>();
? ? private Button mBtn;
?
? ? @Override
? ? protected void onCreate(Bundle savedInstanceState) {
? ? ? ? super.onCreate(savedInstanceState);
? ? ? ? setContentView(R.layout.activity_main);
? ? ? ? getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN );//這句讓view 頂?shù)巾?
? ? ? ? getWindow().setStatusBarColor(Color.TRANSPARENT);//這句讓狀態(tài)欄透明
? ? ? ? initVew();
? ? }
?
? ? private void initVew() {
? ? ? ? mPicture1=findViewById(R.id.picture1);
? ? ? ? mPicture2=findViewById(R.id.picture2);
? ? ? ? mPicture3=findViewById(R.id.picture3);
? ? ? ? mBtn=findViewById(R.id.makesure);
?
? ? ? ? mPicture1.setOnClickListener(this);
? ? ? ? mPicture2.setOnClickListener(this);
? ? ? ? mPicture3.setOnClickListener(this);
? ? ? ? mBtn.setOnClickListener(this);
? ? }
?
? ? @Override
? ? public void onClick(View view) {
? ? ? ? switch (view.getId()) {
? ? ? ? ? ? case R.id.picture1:
? ? ? ? ? ? ? ? photoAndAll(mPicture1);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.picture2:
? ? ? ? ? ? ? ? photoAndAll(mPicture2);
? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.picture3:
? ? ? ? ? ? ? ? photoAndAll(mPicture3);
? ? ? ? ? ? ? ? break;
? ? ? ? ? ? case R.id.makesure:
?
? ? ? ? ? ? ? ? break;
? ? ? ? }
? ? }
?
?
? ? private ?void photoAndAll(ImageView imageView){
? ? ? ? PictureSelector.create(this)
? ? ? ? ? ? ? ? .openGallery(SelectMimeType.ofImage())
? ? ? ? ? ? ? ? .setImageEngine(GlideEngine.createGlideEngine()).setMaxSelectNum(1)
? ? ? ? ? ? ? ? .forResult(new OnResultCallbackListener<LocalMedia>() {
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onResult(ArrayList<LocalMedia> result) {
? ? ? ? ? ? ? ? ? ? ? ? Log.e("leo","圖片路徑"+result.get(0).getPath());
? ? ? ? ? ? ? ? ? ? ? ? Log.e("leo","絕對(duì)路徑"+result.get(0).getRealPath());
? ? ? ? ? ? ? ? ? ? ? ? Glide.with(MainActivity.this).load(result.get(0).getPath()).into(imageView);
? ? ? ? ? ? ? ? ? ? ? ? //將bitmap圖片傳入后端
? ? ? ? ? ? ? ? ? ? ? ? //imageUpLoad(result.get(0).getRealPath());
? ? ? ? ? ? ? ? ? ? ? ? SubmitPicture(result.get(0).getRealPath());
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? public void onCancel() {
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? });
? ? }
?
? ? public static void imageUpLoad(String localPath) {
? ? ? ? MediaType MEDIA_TYPE_PNG = MediaType.parse("image/png");
? ? ? ? OkHttpClient client = new OkHttpClient();
?
? ? ? ? MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM);
?
? ? ? ? File f = new File(localPath);
? ? ? ? builder.addFormDataPart("files", f.getName(), RequestBody.create(MEDIA_TYPE_PNG, f));
? ? ? ? final MultipartBody requestBody = builder.build();
? ? ? ? //構(gòu)建請(qǐng)求
? ? ? ? final Request request = new Request.Builder()
? ? ? ? ? ? ? ? .url("后端接口")//地址
? ? ? ? ? ? ? ? .post(requestBody)//添加請(qǐng)求體
? ? ? ? ? ? ? ? .build();
?
? ? ? ? client.newCall(request).enqueue(new okhttp3.Callback() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onFailure(Call call, IOException e) {
? ? ? ? ? ? ? ? System.out.println("上傳失敗:e.getLocalizedMessage() = " + e.getLocalizedMessage());
? ? ? ? ? ? }
?
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onResponse(Call call, Response response) throws IOException {
?
? ? ? ? ? ? ? ? pmodel model = JSON.parseObject(response.body().string(), pmodel.class);
? ? ? ? ? ? ? ? Log.e("leo",JSON.toJSONString(model));
? ? ? ? ? ? ? ? pmodel model2 = JSON.parseObject(JSON.toJSONString(model), pmodel.class);
? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? });
? ? }
?
?
? ? private void ?SubmitPicture(String path){
? ? ? ? String ?url = "后端接口";
? ? ? ? String token="token,沒(méi)有要求則無(wú)需寫(xiě)入";
? ? ? ? RequestParams params = new RequestParams(url);
? ? ? ? params.addHeader("Authorization",token);
?
? ? ? ? //設(shè)置表單傳送
? ? ? ? params.setMultipart(true);
? ? ? ? params.addBodyParameter("file",new File(path));
? ? ? ? params.addParameter("fileType","PICTURE");
? ? ? ? x.http().post(params, new Callback.CommonCallback<String>() {
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onSuccess(String result) {
? ? ? ? ? ? ? ? Log.e("leo","2222"+params);
? ? ? ? ? ? ? ? Toast.makeText(MainActivity.this, "上傳成功!", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? Log.e("leo","返回"+result);
? ? ? ? ? ? ? ? pmodel model = JSON.parseObject(result, pmodel.class);
? ? ? ? ? ? ? ? PictureArr.add(model.getData().get(0).getUrl());
? ? ? ? ? ? }
?
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onError(Throwable ex, boolean isOnCallback) {
? ? ? ? ? ? ? ? Log.e("leo","失敗"+ex.getMessage());
? ? ? ? ? ? }
?
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onCancelled(CancelledException cex) {
?
? ? ? ? ? ? }
?
? ? ? ? ? ? @Override
? ? ? ? ? ? public void onFinished() {
? ? ? ? ? ? }
? ? ? ? });
? ? }
?
}

說(shuō)明與解析:這邊我們通過(guò)點(diǎn)擊上傳ImageView,則利用pictureSelector進(jìn)行相冊(cè)照片的選取,即photoAndAll方法進(jìn)行選取,并且利用Glide(圖片加載工具類)進(jìn)行UI界面的顯示,最后利用兩種方法(imageUpload和SubmitPicture)提交上傳圖片,兩種方法自行選取即可。

第三方pictureSelector的引入,以及Glide圖片加載,xutils工具類的引入可參考如下:

?// PictureSelector 基礎(chǔ) (必須)
? ? implementation 'io.github.lucksiege:pictureselector:v3.0.5'
? ? // 圖片壓縮 (按需引入)
? ? implementation 'io.github.lucksiege:compress:v3.0.5'
? ? // 圖片裁剪 (按需引入)
? ? implementation 'io.github.lucksiege:ucrop:v3.0.5'
? ? // 自定義相機(jī) (按需引入)
? ? implementation 'io.github.lucksiege:camerax:v3.0.5'
? ? // ? ?加載網(wǎng)絡(luò)圖片
? ? implementation 'com.github.bumptech.glide:glide:4.10.0'
? ? annotationProcessor 'com.github.bumptech.glide:compiler:4.10.0'
? ? //網(wǎng)絡(luò)請(qǐng)求
? ? implementation 'org.xutils:xutils:3.8.4'

xutils工具的使用需要?jiǎng)?chuàng)建自定義Application進(jìn)行初始化

public class BaseApplication extends Application {
? ? public static Handler sHandler;
? ? public static Context sContext = null;
? ? @Override
? ? public void onCreate() {
? ? ? ? super.onCreate();
?
? ? ? ? //初始化xUtils
? ? ? ? x.Ext.init(this);
? ? ? ? x.Ext.setDebug(BuildConfig.DEBUG);
?
? ? }

}

Android圖片上傳下載總結(jié)與擴(kuò)展

至此,簡(jiǎn)單的Android圖片上傳就完成了,圖片的下載與只相似,只需要利用Xutils網(wǎng)絡(luò)工具進(jìn)行接口訪問(wèn),拿到圖片利用Glide或者其他方式進(jìn)行顯示和應(yīng)用即可。

圖片的上傳下載與一些文件的上傳下載類似,本demo只進(jìn)行了單圖上傳的實(shí)例,實(shí)際上pictureSelector工具類十分強(qiáng)大,對(duì)于多圖多選,圖片壓縮,視頻選取都可以一句代碼搞定,這樣就可以很方便的實(shí)現(xiàn)多圖上傳,視頻上傳等其他功能,前提是后端小伙伴提供給你的接口支持相關(guān)上傳下載。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論