這篇文章主要介紹了Android如何實現(xiàn)圖片選擇上傳功能,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
創(chuàng)新互聯(lián)公司主要業(yè)務(wù)有網(wǎng)站營銷策劃、成都做網(wǎng)站、網(wǎng)站建設(shè)、微信公眾號開發(fā)、小程序設(shè)計、H5網(wǎng)站設(shè)計、程序開發(fā)等業(yè)務(wù)。一次合作終身朋友,是我們奉行的宗旨;我們不僅僅把客戶當(dāng)客戶,還把客戶視為我們的合作伙伴,在開展業(yè)務(wù)的過程中,公司還積累了豐富的行業(yè)經(jīng)驗、網(wǎng)絡(luò)營銷推廣資源和合作伙伴關(guān)系資源,并逐漸建立起規(guī)范的客戶服務(wù)和保障體系。
效果圖:
添加依賴:
選擇圖片:compile 'com.lzy.widget:imagepicker:0.5.4'
github地址:https://github.com/jeasonlzy/ImagePicker
上傳文件:compile 'com.zhy:okhttputils:2.6.2'
github地址:https://github.com/hongyangAndroid/okhttputils
MainActivity.java
package com.yechaoa.uploadimage; import android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.GridLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.View; import android.widget.AdapterView; import com.lzy.imagepicker.ImagePicker; import com.lzy.imagepicker.bean.ImageItem; import com.lzy.imagepicker.ui.ImageGridActivity; import com.lzy.imagepicker.ui.ImagePreviewDelActivity; import com.lzy.imagepicker.view.CropImageView; import java.util.ArrayList; import java.util.List; import okhttp3.Call; public class MainActivity extends AppCompatActivity implements ImagePickerAdapter.OnRecyclerViewItemClickListener{ public static final int IMAGE_ITEM_ADD = -1; public static final int REQUEST_CODE_SELECT = 100; public static final int REQUEST_CODE_PREVIEW = 101; private ImagePickerAdapter adapter; private ArrayList<ImageItem> selImageList; //當(dāng)前選擇的所有圖片 private int maxImgCount = 8; //允許選擇圖片最大數(shù) private HttpUtil httpUtil; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); httpUtil = new HttpUtil(); findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { uploadImage(selImageList); } }); //最好放到 Application oncreate執(zhí)行 initImagePicker(); initWidget(); } private void initImagePicker() { ImagePicker imagePicker = ImagePicker.getInstance(); imagePicker.setImageLoader(new GlideImageLoader()); //設(shè)置圖片加載器 imagePicker.setShowCamera(true); //顯示拍照按鈕 imagePicker.setCrop(true); //允許裁剪(單選才有效) imagePicker.setSaveRectangle(true); //是否按矩形區(qū)域保存 imagePicker.setSelectLimit(maxImgCount); //選中數(shù)量限制 imagePicker.setMultiMode(false); //多選 imagePicker.setStyle(CropImageView.Style.RECTANGLE); //裁剪框的形狀 imagePicker.setFocusWidth(800); //裁剪框的寬度。單位像素(圓形自動取寬高最小值) imagePicker.setFocusHeight(800); //裁剪框的高度。單位像素(圓形自動取寬高最小值) imagePicker.setOutPutX(1000); //保存文件的寬度。單位像素 imagePicker.setOutPutY(1000); //保存文件的高度。單位像素 } private void initWidget() { RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); selImageList = new ArrayList<>(); adapter = new ImagePickerAdapter(this, selImageList, maxImgCount); adapter.setOnItemClickListener(this); recyclerView.setLayoutManager(new GridLayoutManager(this, 4)); recyclerView.setHasFixedSize(true); recyclerView.setAdapter(adapter); } private SelectDialog showDialog(SelectDialog.SelectDialogListener listener, List<String> names) { SelectDialog dialog = new SelectDialog(this, R.style.transparentFrameWindowStyle, listener, names); if (!this.isFinishing()) { dialog.show(); } return dialog; } @Override public void onItemClick(View view, int position) { switch (position) { case IMAGE_ITEM_ADD: List<String> names = new ArrayList<>(); names.add("拍照"); names.add("相冊"); showDialog(new SelectDialog.SelectDialogListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { switch (position) { case 0: // 直接調(diào)起相機 //打開選擇,本次允許選擇的數(shù)量 ImagePicker.getInstance().setSelectLimit(maxImgCount - selImageList.size()); Intent intent = new Intent(MainActivity.this, ImageGridActivity.class); intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS,true); // 是否是直接打開相機 startActivityForResult(intent, REQUEST_CODE_SELECT); break; case 1: //打開選擇,本次允許選擇的數(shù)量 ImagePicker.getInstance().setSelectLimit(maxImgCount - selImageList.size()); Intent intent1 = new Intent(MainActivity.this, ImageGridActivity.class); startActivityForResult(intent1, REQUEST_CODE_SELECT); break; default: break; } } }, names); break; default: //打開預(yù)覽 Intent intentPreview = new Intent(this, ImagePreviewDelActivity.class); intentPreview.putExtra(ImagePicker.EXTRA_IMAGE_ITEMS, (ArrayList<ImageItem>) adapter.getImages()); intentPreview.putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, position); intentPreview.putExtra(ImagePicker.EXTRA_FROM_ITEMS,true); startActivityForResult(intentPreview, REQUEST_CODE_PREVIEW); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == ImagePicker.RESULT_CODE_ITEMS) { //添加圖片返回 if (data != null && requestCode == REQUEST_CODE_SELECT) { ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS); if (images != null){ selImageList.addAll(images); adapter.setImages(selImageList); } } } else if (resultCode == ImagePicker.RESULT_CODE_BACK) { //預(yù)覽圖片返回 if (data != null && requestCode == REQUEST_CODE_PREVIEW) { ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_IMAGE_ITEMS); if (images != null){ selImageList.clear(); selImageList.addAll(images); adapter.setImages(selImageList); } } } } private String url="http..."; private void uploadImage(ArrayList<ImageItem> pathList) { httpUtil.postFileRequest(url, null, pathList, new MyStringCallBack() { @Override public void onError(Call call, Exception e, int id) { super.onError(call, e, id); } @Override public void onResponse(String response, int id) { super.onResponse(response, id); //返回圖片的地址 } }); } }
感謝你能夠認真閱讀完這篇文章,希望小編分享的“Android如何實現(xiàn)圖片選擇上傳功能”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!
網(wǎng)站題目:Android如何實現(xiàn)圖片選擇上傳功能
新聞來源:http://jinyejixie.com/article2/gpeioc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站制作、網(wǎng)站營銷、企業(yè)網(wǎng)站制作、靜態(tài)網(wǎng)站、App設(shè)計、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)