Android實(shí)現(xiàn)上傳圖片功能
更新時(shí)間:2021年09月12日 11:57:01 作者:Simon66991
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)上傳圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
本文實(shí)例為大家分享了Android實(shí)現(xiàn)上傳圖片功能的具體代碼,供大家參考,具體內(nèi)容如下
設(shè)定拍照返回的圖片路徑
/**
* 設(shè)定拍照返回的圖片路徑
* @param image 圖片路徑
* @param i 約定值
*/
protected void image(String image, int i) {
//創(chuàng)建file對(duì)象,用于存儲(chǔ)拍照后的圖片,這也是拍照成功后的照片路徑
outputImage = new File(getExternalCacheDir(),image);
try {
//判斷文件是否存在,存在刪除,不存在創(chuàng)建
if (outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
//相機(jī)拍照返回圖片路徑
Uri photoUri;
//判斷當(dāng)前Android版本
if(Build.VERSION.SDK_INT>=24){
photoUri = FileProvider.getUriForFile(TextActivity.this,"包名.FileProvider",outputImage);
}else {
photoUri = Uri.fromFile(outputImage);
}
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, i);
}
調(diào)用系統(tǒng)相機(jī)拍照接受返回的圖片路徑
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == IMAGE_Y) {
getImageView(binding.imageY,"0");
}
if (requestCode == IMAGE_Q) {
getImageView(binding.imageQ,"1");
}
}
}
上傳圖片
/**
* 上傳圖片
* @param view 圖片展示 view
*/
protected void getImageView(@NotNull ImageView view, String type) {
Bitmap photo = BitmapFactory.decodeFile(outputImage.getAbsolutePath());
view.setImageBitmap(photo);
int direction = 0;
try {
ExifInterface exif = new ExifInterface(String.valueOf(outputImage));
direction = Integer.parseInt(exif.getAttribute(ExifInterface.TAG_ORIENTATION));
} catch (IOException e) {
e.printStackTrace();
}
Matrix matrix = new Matrix();
Uri uri = Uri.fromFile(outputImage);
String f = uri.getPath();
Bitmap b = rotateBitmap(getBitmapFromUrl(f,900,1200),0);
switch (direction) {
case 1:
Log.d("圖片方向","頂部,左側(cè)(水平/正常)");
b = rotateBitmap(getBitmapFromUrl(f,900,1200),0);
break;
case 2:
b = rotateBitmap(getBitmapFromUrl(f,900,1200),0);
Log.d("圖片方向","頂部,右側(cè)(水平鏡像)");
break;
case 3:
b = rotateBitmap(getBitmapFromUrl(f,900,1200),180);
Log.d("圖片方向","底部,右側(cè)(旋轉(zhuǎn)180)");
break;
case 4:
matrix.postScale(1, -1);
b = Bitmap.createBitmap(b,0,0,900,1200,matrix,true);
Log.d("圖片方向","底部,左側(cè)(垂直鏡像)");
break;
case 5:
matrix.postScale(-1, 1);
b = rotateBitmap(getBitmapFromUrl(f,900,1200),270);
b = Bitmap.createBitmap(b,0,0,900,1200,matrix,true);
Log.d("圖片方向","左側(cè),頂部(水平鏡像并順時(shí)針旋轉(zhuǎn)270)");
break;
case 6:
b = rotateBitmap(getBitmapFromUrl(f,900,1200),90);
Log.d("圖片方向","右側(cè),頂部(順時(shí)針旋轉(zhuǎn)90)");
break;
case 7:
matrix.postScale(-1, 1);
b = rotateBitmap(getBitmapFromUrl(f,900,1200),90);
b = Bitmap.createBitmap(b,0,0,900,1200,matrix,true);
Log.d("圖片方向","右側(cè),底部(水平鏡像,順時(shí)針旋轉(zhuǎn)90度)");
break;
case 8:
b = rotateBitmap(getBitmapFromUrl(f,900,1200),270);
Log.d("圖片方向","左側(cè),底部(順時(shí)針旋轉(zhuǎn)270)");
break;
default:
break;
}
try {
File files = new File(new URI(uri.toString()));
saveBitmap(b,files);
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
String file = uploadImage(networkApi.getFormal() + "setImage", uri.getPath(), code, userId);
TextBase.FileInfo fileInfo = new TextBase.FileInfo();
fileInfo.setFilePath(file);
mFileInfos.add(fileInfo);
model.load(file,type);
} catch (IOException | JSONException e) {
e.printStackTrace();
}
}
/**
* 上傳圖片
* @param url 上傳接口路徑
* @param imagePath 圖片路徑
* @param code 唯一標(biāo)識(shí)
* @return 服務(wù)器圖片的路徑
* @throws IOException
* @throws JSONException
*/
public static String uploadImage(String url, String imagePath, String code, String userId) throws IOException, JSONException {
OkHttpClient okHttpClient = new OkHttpClient();
Log.d("imagePath", imagePath);
File file = new File(imagePath);
RequestBody image = RequestBody.create(MediaType.parse("image/jpg"), file);
RequestBody requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("file", imagePath, image)
.addFormDataPart("fileOid", code)
.addFormDataPart("userId", userId)
.build();
Request request = new Request.Builder()
.url(url)
.addHeader("Authorization",令牌)
.post(requestBody)
.build();
Response response = okHttpClient.newCall(request).execute();
JSONObject jsonObject = new JSONObject(response.body().string());
return jsonObject.optString("msg");
}
其他工具類
/**
* 壓縮圖片的方法
* @param url
* @param width
* @param height
* @return
*/
public static Bitmap getBitmapFromUrl(String url, double width, double height) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; // 設(shè)置了此屬性一定要記得將值設(shè)置為false
Bitmap bitmap = BitmapFactory.decodeFile(url);
// 防止OOM發(fā)生
options.inJustDecodeBounds = false;
int mWidth = bitmap.getWidth();
int mHeight = bitmap.getHeight();
Matrix matrix = new Matrix();
float scaleWidth = 1;
float scaleHeight = 1;
// 按照固定寬高進(jìn)行縮放
if(mWidth <= mHeight) {
scaleWidth = (float) (width/mWidth);
scaleHeight = (float) (height/mHeight);
} else {
scaleWidth = (float) (height/mWidth);
scaleHeight = (float) (width/mHeight);
}
// 按照固定大小對(duì)圖片進(jìn)行縮放
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, mWidth, mHeight, matrix, true);
// 用完了記得回收
bitmap.recycle();
return newBitmap;
}
/**
* Android保存Bitmap到文件
* @param bitmap
* @param file
* @return
*/
public static boolean saveBitmap(Bitmap bitmap, File file) {
if (bitmap == null)
return false;
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
return true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return false;
}
/**
* 旋轉(zhuǎn)圖片
* @param bitmap 圖片
* @param rotate 角度
* @return
*/
public static Bitmap rotateBitmap(Bitmap bitmap, int rotate) {
if (bitmap == null)
return null;
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Matrix mtx = new Matrix();
mtx.postRotate(rotate);
return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android使用post方式上傳圖片到服務(wù)器的方法
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android Retrofit 2.0框架上傳圖片解決方案
- Android實(shí)現(xiàn)上傳圖片至java服務(wù)器
- android上傳圖片到PHP的過程詳解
- Android 開發(fā) 使用WebUploader解決安卓微信瀏覽器上傳圖片中遇到的bug
- Android基于OkHttp實(shí)現(xiàn)下載和上傳圖片
- Android 通過Base64上傳圖片到服務(wù)器實(shí)現(xiàn)實(shí)例
- Android異步上傳圖片到PHP服務(wù)器
- Android使用OkHttp上傳圖片的實(shí)例代碼
相關(guān)文章
Android采取ContentObserver方式自動(dòng)獲取驗(yàn)證碼
這篇文章主要為大家詳細(xì)介紹了Android采取ContentObserver方式自動(dòng)獲取驗(yàn)證碼,感興趣的小伙伴們可以參考一下2016-08-08
Android中使用自定義ViewGroup的總結(jié)
本篇文章主要介紹了Android中使用自定義ViewGroup的總結(jié),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-01-01
Android實(shí)現(xiàn)無標(biāo)題欄全屏的方法
這篇文章主要介紹了Android實(shí)現(xiàn)無標(biāo)題欄全屏的三種方法,感興趣的小伙伴們可以參考一下2016-07-07
Android 三種動(dòng)畫詳解及簡單實(shí)例
這篇文章主要介紹了Android 三種動(dòng)畫詳解及簡單實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-04-04

