知識
不管是網(wǎng)站,軟件還是小程序,都要直接或間接能為您產(chǎn)生價值,我們在追求其視覺表現(xiàn)的同時,更側(cè)重于功能的便捷,營銷的便利,運營的高效,讓網(wǎng)站成為營銷工具,讓軟件能切實提升企業(yè)內(nèi)部管理水平和效率。優(yōu)秀的程序為后期升級提供便捷的支持!
android選擇圖片拍照并剪切照片上傳到服務器
發(fā)表時間:2020-11-6
發(fā)布人:葵宇科技
瀏覽次數(shù):60
界面調(diào)用方法: pickerPicture 和 pickerPictureOk 為父類方法,
父類方法中調(diào)用上傳,界面中只需要處理這兩個方法即可;
[img]http://img.blog.csdn.net/20141231160350949?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaW50YmlyZA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
父類中處理打開照片,選擇完成 和上傳到服務器 調(diào)用 上傳完成通知界面
[img]http://img.blog.csdn.net/20141231160645840?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaW50YmlyZA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
[img]http://img.blog.csdn.net/20141231160919470?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaW50YmlyZA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
選擇圖片并剪切,4.4的手機不同于其他
package com.idonoo.shareCar.ui.commom.activitys; import java.io.File; import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.os.AsyncTask; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.widget.TextView; import com.idonoo.frame.model.ImageStorage; import com.idonoo.frame.utils.ImageStoreHelper; import com.idonoo.frame.utils.ImageUtil; import com.idonoo.shareCar.R; import com.idonoo.shareCar.app.IntentExtra; import com.idonoo.shareCar.app.IntentResult; import com.idonoo.shareCar.uiframe.BaseActivity; import com.idonoo.shareCar.utils.PhotoUtility; import com.intbird.soft.utils.BitmapHelper; import com.intbird.soft.utils.CacheHelper; import com.intbird.soft.utils.PhoneHelper; public class TakeImageActivity extends BaseActivity { private int viewId; ImageStoreHelper.ImageOptions imageOptions; private ImageStorage imageFile; private String orignalFileUrl=""; private String cutedFileUrl=""; private String tempNewFilename=""; private CopyImageTask task; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.layout_popup_take_image); initUI(); initData(); CacheHelper.initCache("Idonoo");//actually only need once ,but i used when need it; } @Override protected void initUI() { super.initUI(); super.initActionBar(); setViewClickListener(listener, R.id.btn_select, R.id.btn_take_photo, R.id.cancel); } @Override protected void initData() { viewId=getIntent().getIntExtra(IntentExtra.EXTRA_IMAGESOTRE_VIEW_ID, 0); imageFile=(ImageStorage) getIntent().getSerializableExtra(IntentExtra.EXTRA_IMAGESOTRE_STORE_FILE); orignalFileUrl=imageFile.getFilePath(); imageOptions=imageFile.getOptions(); setSheetView(); } private void setSheetView() { int sheetText=getIntent().getIntExtra(IntentExtra.EXTRA_IMAGESOTRE_SHEET_TEXT, 0); if(sheetText!=0){ TextView tvTip = (TextView) findViewById(R.id.tv_tip_text); tvTip.setText(getString(sheetText)); tvTip.setVisibility(View.VISIBLE); } } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("orignalFileUrl", orignalFileUrl); outState.putString("cutedFileUrl", cutedFileUrl); outState.putString("tempNewFilename", tempNewFilename); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); orignalFileUrl=savedInstanceState.getString("orignalFileUrl"); cutedFileUrl=savedInstanceState.getString("cutedFileUrl"); tempNewFilename=savedInstanceState.getString("tempNewFilename"); } View.OnClickListener listener = new View.OnClickListener() { @Override public void onClick(View v) { switch (v.getId()) { case R.id.btn_select: { try{ take(); }catch(Exception ex){ showToast("打開手機拍照失敗\n請嘗試從相冊選擇~"); } } break; case R.id.btn_take_photo: { try{ showToast("請從系統(tǒng)圖庫中選擇照片哦~"); select(); }catch(Exception ex){ showToast("從相冊選擇失敗\n請使用其他選圖軟件哦~"); } } break; case R.id.cancel: { finish(false); } break; } } }; private void take() { tempNewFilename=imageFile.getFileName()+System.currentTimeMillis()+".jpg"; orignalFileUrl=CacheHelper.createCameraFile(tempNewFilename).getAbsolutePath(); Intent intent = PhotoUtility.gotoCamera(orignalFileUrl); startActivityForResult(intent, IntentResult.RS_CAMERA); } private void select() { Intent intent = PhotoUtility.gotoGallery(); if(PhoneHelper.isVersionHight(19)){ intent=PhotoUtility.gotoGalleryPick(); } startActivityForResult(intent, IntentResult.RS_GALLERY); } private void crop(){ tempNewFilename=imageFile.getFileName()+System.currentTimeMillis()+".jpg"; cutedFileUrl=CacheHelper.createImageFile(tempNewFilename).getAbsolutePath(); try{ Intent intent = PhotoUtility.cropImageFile(orignalFileUrl,cutedFileUrl,imageOptions.cropWidth,imageOptions.cropHeight); startActivityForResult(intent, IntentResult.RS_CROP); }catch(Exception ex){ showToast("手機中缺少圖片剪切程序\n將壓縮上傳原圖,請保證圖片大小合適"); cutedFileUrl=orignalFileUrl; task=new CopyImageTask(imageOptions); task.execute(); } } private void cropOk() { task=new CopyImageTask(imageOptions); task.execute(); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { switch (requestCode) { case IntentResult.RS_CAMERA: { crop(); } break; case IntentResult.RS_GALLERY: { String tempGetUrl=""; try{ tempGetUrl = ImageUtil.getPathFromUri(data.getData()); }catch(Exception ex){ tempGetUrl=""; showToast("貌似選擇的圖片出了點問題\n請重新選取圖片"); } if(TextUtils.isEmpty(tempGetUrl)){ showToast("貌似選擇的圖片出了點問題\n請重新選取圖片"); return ; } orignalFileUrl = tempGetUrl; crop(); } break; case IntentResult.RS_CROP: { cropOk(); } break; } } } private class CopyImageTask extends AsyncTask<Void, Void, File>{ private ImageStoreHelper.ImageOptions options; public CopyImageTask(ImageStoreHelper.ImageOptions imageOptions){ this.options=imageOptions; showProgress("正在壓縮圖片"); } @Override protected File doInBackground(Void... params) { Bitmap croped = BitmapHelper.getBitmapFromFile(cutedFileUrl, options.cropWidth,options.cropHeight); //如果經(jīng)過剪切后的圖片大小為空,說明剪切失敗了,則嘗試獲取原圖 if(croped==null){ croped = BitmapHelper.getBitmapFromFile(orignalFileUrl, options.cropWidth,options.cropHeight); } //如果截取原圖失敗,將原圖地址返回 File newFile; if(croped==null){ newFile=new File(orignalFileUrl); return newFile; } Bitmap bitmap = BitmapHelper.compressBitmap(croped, options.maxKb); newFile = CacheHelper.copyBitmapToCache(bitmap,tempNewFilename); return newFile; } @Override protected void onPostExecute(File file) { super.onPostExecute(file); dismissProgress(); if(file==null||!file.exists()){ showToast("復制選中文件到Idonoo文件夾下失敗!\n檢查圖片或內(nèi)存卡是否可用~"); return ; } if(file.length()<10*1024){ file.delete(); showToast("上傳圖片小于10KB,請重新截取大圖~"); return ; } imageFile.setFilePath(file.getAbsolutePath()); finish(true); } } private void finish(boolean isOk){ if(!isOk) viewId=0; Intent intent=new Intent(); intent.putExtra(IntentExtra.EXTRA_IMAGESOTRE_VIEW_ID, viewId); intent.putExtra(IntentExtra.EXTRA_IMAGESOTRE_STORE_FILE, imageFile); setResult(Activity.RESULT_OK,intent); if(task!=null&&!task.isCancelled()) task.cancel(true); finish(); } }
package com.idonoo.shareCar.utils; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.provider.MediaStore; import java.io.File; public class PhotoUtility { public static Intent gotoGallery() throws NullPointerException{ Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null); //ACTION_PICK//ACTION_GET_CONTENT intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); return intent; } public static Intent gotoGalleryPick() throws NullPointerException{ Intent intent = new Intent(Intent.ACTION_PICK, null); //ACTION_PICK//ACTION_GET_CONTENT intent.setData(MediaStore.Images.Media.EXTERNAL_CONTENT_URI); intent.setType("image/*"); return intent; } public static Intent gotoCamera(String filePath) throws NullPointerException{ Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath))); return intent; } public static Intent cropImage(Uri uri, String filePath, int width, int height) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", width); intent.putExtra("aspectY", height); intent.putExtra("outputX", width); intent.putExtra("outputY", height); intent.putExtra("return-data", false); intent.putExtra("noFaceDetection", true); intent.putExtra("scale", true); intent.putExtra("scaleUpIfNeeded", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(filePath))); return intent; } public static Intent cropImage(String filePath, int width, int height) { Uri uri=Uri.fromFile(new File(filePath)); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", width); intent.putExtra("aspectY", height); intent.putExtra("outputX", width); intent.putExtra("outputY", height); intent.putExtra("return-data", false); intent.putExtra("noFaceDetection", true); intent.putExtra("scale", true); intent.putExtra("scaleUpIfNeeded", true); intent.putExtra(MediaStore.EXTRA_OUTPUT,uri); return intent; } public static Intent cropImageFile(String oldPath,String filePath,int width, int height) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(Uri.fromFile(new File(oldPath)), "image/*"); intent.putExtra("crop", "true"); intent.putExtra("aspectX", width); intent.putExtra("aspectY", height); intent.putExtra("outputX", width); intent.putExtra("outputY", height); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("return-data", false); intent.putExtra("noFaceDetection", true); intent.putExtra("scale", true); intent.putExtra("scaleUpIfNeeded", true); intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(new File(filePath))); return intent; } }
相關案例查看更多
相關閱讀
- 報廢車
- 百度小程序
- 昆明小程序設計
- 報廢車拆解系統(tǒng)
- 報廢車管理系統(tǒng)
- 云南網(wǎng)站建設報價
- 網(wǎng)站建設列表網(wǎng)
- 小程序被攻擊
- 云南網(wǎng)站建設公司哪家好
- 汽車回收管理系統(tǒng)
- 小程序的開發(fā)公司
- Web開發(fā)框架
- 貴州小程序開發(fā)
- 迪慶小程序開發(fā)
- 做網(wǎng)站
- 商標注冊
- 汽車報廢回收軟件
- 百度小程序開發(fā)公司
- 云南網(wǎng)站建設方案 doc
- 云南小程序開發(fā)課程
- 小程序表單
- 網(wǎng)站排名優(yōu)化
- 昆明小程序開發(fā)聯(lián)系方式
- 商標
- 網(wǎng)站建設
- 區(qū)塊鏈
- 云南網(wǎng)站建設方法
- 百度小程序開發(fā)
- 云南旅游網(wǎng)站建設
- 云南網(wǎng)站制作