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

Android實現(xiàn)圖片裁剪和上傳

 更新時間:2022年08月12日 08:51:02   作者:我不學習沒人要  
這篇文章主要為大家詳細介紹了Android實現(xiàn)圖片的裁剪和上傳,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了Android實現(xiàn)圖片的裁剪和上傳的具體代碼,供大家參考,具體內(nèi)容如下

1、開發(fā)工具與關鍵技術:Eclipse、AndroidStudio
2、撰寫時間:2020年06月18日

接著上一次,調(diào)用系統(tǒng)的相機。上一次,我們已經(jīng)拿到了圖片的uri。接下來,就要進行圖片的裁剪和上傳啦!其實圖片的裁剪和上傳比較簡單。如何簡單呢?好,我們來看代碼。

1、首先,配置maven,這里是使用uCrop裁剪圖片

//圖像裁剪 , 需要先配置 maven { url "https://jitpack.io" }
implementation 'com.github.yalantis:ucrop:2.2.4'
//加載層 需要先配置 maven
implementation 'com.github.ForgetAll:LoadingDialog:v1.1.2'

2、其次,在清單文件中添加。注意:fullSensor看個人的用法,有些版本太高,不可使用

<activity
? ? android:name="com.yalantis.ucrop.UCropActivity"
? ? android:screenOrientation="fullSensor"
? ? android:theme="@style/Theme.AppCompat.Light.NoActionBar"/>

3、裁剪的方法

//開始圖片裁剪 使用UCrop
private void startPhotoZoom(Uri uri) {
? ? ? ? //裁剪后保存到文件中
? ? ? ? Uri cropFileUri = Uri.fromFile(mCropFile);
? ? ? ? UCrop uCrop = UCrop.of(uri, cropFileUri);//源文件url,裁剪后輸出文件uri
? ? ? ? UCrop.Options options = new UCrop.Options();
? ? ? ? //設置裁剪圖片可操作的手勢
? ? ? ? options.setAllowedGestures(UCropActivity.SCALE, UCropActivity.ROTATE, UCropActivity.ALL);
? ? ? ? //是否能調(diào)整裁剪框
? ? ? ? options.setFreeStyleCropEnabled(false);
? ? ? ? uCrop.withOptions(options);
? ? ? ? //設置比例為1:1
? ? ? ? uCrop.withAspectRatio(1, 1);
? ? ? ? //注意?。。?!Fragment中使用uCrop 必須這樣,否則Fragment的onActivityResult接收不到回調(diào)
? ? ? ? uCrop.start(mActivityContext, this);
? ? }

4、在上次的Activity方法中調(diào)用裁剪的方法

public class UserFragment extends Fragment {

? ? private static final int IMAGE_REQUEST_CODE = 100;
? ? private static final int IMAGE_REQUEST_CODE_GE7 = 101;
? ? private static final int CAMERA_REQUEST_CODE = 104;
? ? private static final int REQUEST_EXTERNAL_STORAGE_CODE = 200;
? ? private Activity mActivityContext;//獲取上下文
? ? private MyApplication myApplication;//獲取myApplication中的BestLogin對象
? ? private BestLogin member;//用戶對象
? ? private File mGalleryFile;//存放圖庫選擇是返回的圖片
? ? private File mCameraFile;//存放相機的圖片
? ? private File mCropFile;//存放圖像裁剪的圖片
private LoadingDialog loadingDialog;//加載層
**//注意:這個方法里面有些代碼是上次的系統(tǒng)調(diào)用相機的,請留意和上次代碼是否有重復**
@Override
? ? public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
? ? ? ? super.onActivityResult(requestCode, resultCode, data);
? ? ? ? if (resultCode == Activity.RESULT_OK && (data != null || requestCode == CAMERA_REQUEST_CODE)) {
? ? ? ? ? ? switch (requestCode) {
? ? ? ? ? ? ? ? case IMAGE_REQUEST_CODE://版本<7.0 ?圖庫返回
? ? ? ? ? ? ? ? ? ? //獲取圖片的全路徑
? ? ? ? ? ? ? ? ? ? Uri uri = data.getData();
? ? ? ? ? ? ? ? ? ? Log.e("ImagUri", uri.getPath());
? ? ? ? ? ? ? ? ? ? **//進行圖像裁剪 這里需要調(diào)用圖片裁剪的方法**
? ? ? ? ? ? ? ? ? ? startPhotoZoom(uri);
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? ? ? case IMAGE_REQUEST_CODE_GE7://版本>= 7.0 圖庫返回
? ? ? ? ? ? ? ? ? ? //獲取文件路徑
? ? ? ? ?String strPath = GetImagePath.getPath(mActivityContext, data.getData());
? ? ? ? ? ? ? ? ? ? if (Tools.isNotNull(strPath)) {
? ? ? ? ? ? ? ? ? ? ? ? File imgFile = new File(strPath);
? ? ? ? ? ? ? ? ? ? ? ? //通過FileProvider創(chuàng)建一個content類型的Uri
? ? ? ? ? ? ? ? ? ? ? ?Uri dataUri = FileProvider.getUriForFile(mActivityContext, "com.gx.reservation.fileprovider", imgFile);
? ? ? ? ? ? ? ? ? ? ? ? Log.e("ImagUri", dataUri.getPath());
? ? ? ? ? ? ? ? ? ? ? ? **//進行圖像裁剪 這里需要調(diào)用圖片裁剪的方法**
? ? ? ? ? ? ? ? ? ? ? ? startPhotoZoom(dataUri);
? ? ? ? ? ? ? ? ? ? } else {
? ? ? ? ? ? ? ? ? ? ? ? Toast.makeText(mActivityContext, "選擇圖片失敗", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? break;
**//這個還算比較重要的代碼**
? ? ? ? ? ? ? ? case CAMERA_REQUEST_CODE://相機的返回
? ? ? ? ? ? ? ? ? ? Uri inputUrl;
? ? ? ? ? ? ? ? ? ? if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
? ? ? ? ? ? ? ? ? ? ? ? //Android7.0及以上
? ? ? ? ? ? ? ? ? ? ? ? //通過FileProvider創(chuàng)建一個content類型的Uri ? ? ? ? ? ? ? ? ? ? inputUrl=FileProvider.getUriForFile(mActivityContext,"com.gx.reservation.fileprovider",mCameraFile);
? ? ? ? ? ? ? ? ? ? }else {
? ? ? ? ? ? ? ? ? ? ? ? //Android7.0以下
? ? ? ? ? ? ? ? ? ? ? ? inputUrl=Uri.fromFile(mCameraFile);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? //啟動圖片裁剪
? ? ? ? ? ? ? ? ? ? if (inputUrl!=null){
? ? ? ? ? ? ? ? ? ? ? ? startPhotoZoom(inputUrl);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? break;
**//核心代碼,其他的代碼上次調(diào)用系統(tǒng)的相機都有用到**
? ? ? ? ? ? ? ? case UCrop.REQUEST_CROP://Ucrop裁剪返回
? ? ? ? ? ? ? ? ? ? Uri resultUri = UCrop.getOutput(data);
? ? ? ? ? ? ? ? ? ? if (resultUri != null) {
? ? ? ? ? ? ? ? ? ? ? ? //uri轉文件路徑
? ? ? ? ? ? ? ? ? ? ? ? String strPathCrop = GetImagePath.getPath(mActivityContext, resultUri);
? ? ? ? ? ? ? ? ? ? ? ? if (Tools.isNotNull(strPathCrop)) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? File fileUp = new File(strPathCrop);
? ? ? ? ? ? ? ? ? ? ? ? ? ? if (fileUp.exists()) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //=====上傳文件
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String url = ServiceUrls.getMemberMethodUrl("uploadMemberPicture");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //參數(shù)map
? ? ? ? ? ? ? ? ?Map<String, Object> pMap = new HashMap<>();
? ? ? ? ? ? ? pMap.put("memberId", myApplication.getBestlogin().getLoginid());
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //文件map
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? Map<String, File> fileMap = new HashMap<>();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? fileMap.put("photo", fileUp);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //顯示加載層
? ? ? ? ? ? ? ? ?loadingDialog.setLoadingText("上傳中...").show();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //發(fā)送請求
? ? ? ? ? ? ? ? ? ? OkHttpTool.httpPostWithFile(url, pMap, fileMap, new OkHttpTool.ResponseCallback() {
? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ?public void onResponse(final boolean isSuccess, final int responseCode, String response, Exception exception) {
? ? ? ? ? ? ? ? ? ? mActivityContext.runOnUiThread(new Runnable() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //關閉加載層
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? loadingDialog.close();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String strText = "網(wǎng)絡環(huán)境不佳,請稍后再試";
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (isSuccess && responseCode == 200) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? try {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? JSONObject jsonObject = new JSONObject(response);
? ? ? ? ? ? ? ? ? ? ? ? ? ? int code = jsonObject.getInt("code");
? ? ? ? ? ? ? ? ? ? ? ? ?strText = jsonObject.getString("text");
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? if (code == 200) {
//提示:這里我是用戶的信息的修改,所以把修改后的數(shù)據(jù)設置到BestLogin對象中
? ? ? ? ? ? ? String strData = jsonObject.getString("data");
? ? ? ? ? ?BestLogin newMember = gson.fromJson(strData, BestLogin.class);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?if (newMember != null) { ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? myApplication.setBestlogin(newMember);
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//重新加載本頁面
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? initView();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? } catch (JSONException e) {
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? Toast.makeText(mActivityContext, strText, Toast.LENGTH_LONG).show();
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? });
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? return;
? ? ? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ?Toast.makeText(mActivityContext, "圖片裁剪失敗", Toast.LENGTH_SHORT).show();
? ? ? ? ? ? ? ? ? ? break;
? ? ? ? ? ? }
? ? ? ? } else {
? Toast.makeText(mActivityContext, "操作失敗", Toast.LENGTH_SHORT).show();
? ? ? ? }
? ? }

5、服務端的上傳方法

public Object uploadMemberPicture(int memberId, @RequestParam(value = "photo") MultipartFile mPhoto) {
?? ??? ?JsonReturn jsonReturn=new JsonReturn();
?? ??? ?if(!mPhoto.isEmpty() && mPhoto.getSize()>0) {//判斷文件是否為空
?? ??? ??? ?if(memberId>0) {//判斷memberid
?? ??? ??? ??? ?BestLogin member=this.appMemberService.selectMemberById(memberId);
?? ??? ??? ??? ?if(member!=null) {
?? ??? ??? ??? ??? ?//獲取文件名稱
?? ??? ??? ??? ??? ?String fileName=mPhoto.getOriginalFilename();
?? ??? ??? ??? ??? ?//獲取文件擴展名稱
?? ??? ??? ??? ??? ?String strExt=fileName.substring(fileName.lastIndexOf('.'));
?? ??? ??? ??? ??? ?String phoneName=memberId + "_" + System.currentTimeMillis() + "_" + System.nanoTime() + strExt;
?? ??? ??? ??? ??? ?//保存圖片
?? ??? ??? ??? ??? ?try {
?? ??? ??? ??? ??? ??? ?FileUtils.writeByteArrayToFile(new File(AppSeting.UPLOAD_MEMBER_PHOTO_DIR, phoneName),mPhoto.getBytes());
?? ??? ??? ??? ??? ??? ?//刪除以前的圖片
?? ??? ??? ??? ??? ??? ?if(Tools.isNotNull(member.getPhoto())) {
File oldImage=new File(AppSeting.UPLOAD_MEMBER_PHOTO_DIR,member.getPhoto());
?? ??? ??? ??? ??? ??? ??? ?if(oldImage.exists()) {
?? ??? ??? ??? ??? ??? ??? ??? ?oldImage.delete();
?? ??? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ??? ?//將頭像的文件名稱保存的數(shù)據(jù)庫
?? ??? ??? ??? ??? ??? ?member.setPhoto(phoneName);
?? ??? ??? ??? ??? ??? ?int intR=this.appMemberService.updateMemberById(member);
?? ??? ??? ??? ??? ??? ?if(intR>0) {
?? ??? ?BestLogin memberVo=this.appMemberService.findMemberById(memberId);
?? ??? ??? ??? ??? ??? ??? ?jsonReturn.setCode(200);
?? ??? ??? ??? ??? ??? ??? ?jsonReturn.setText("頭像上傳成功");
?? ??? ??? ??? ??? ??? ??? ?jsonReturn.setData(memberVo);
?? ??? ??? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ??? ??? ?jsonReturn.setCode(300);
?? ??? ??? ??? ??? ??? ??? ?jsonReturn.setText("頭像上傳失敗,稍后再試");
?? ??? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?} catch (IOException e) {
?? ??? ??? ??? ??? ??? ?e.printStackTrace();
?? ??? ??? ??? ??? ??? ?jsonReturn.setCode(300);
?? ??? ??? ??? ??? ??? ?jsonReturn.setText("頭像上傳失敗,稍后再試");
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ?}else {
?? ??? ??? ??? ??? ?jsonReturn.setCode(500);
?? ??? ??? ??? ??? ?jsonReturn.setText("參數(shù)異常");
?? ??? ??? ??? ?}
?? ??? ??? ?}else {
?? ??? ??? ??? ?jsonReturn.setCode(500);
?? ??? ??? ??? ?jsonReturn.setText("參數(shù)異常");
?? ??? ??? ?}
?? ??? ?}else {
?? ??? ??? ?jsonReturn.setCode(500);
?? ??? ??? ?jsonReturn.setText("上傳的頭像為空");
?? ??? ?}
?? ??? ?return gson.toJson(jsonReturn);
?? ?} ?

6、效果圖:

效果二:

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論