編輯:關於Android編程
本文實例為大家分享了Android剪切和上傳圖片的具體代碼,供大家參考,具體內容如下
1、從Android系統相冊選擇一張圖片getImageFromAlbum():
/**
* 從圖庫獲得照片
*/
protected void getImageFromAlbum() {
isImgs = true;
// MainApplication.changeSettingStateus = true;
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");// 相片類型
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 360);
intent.putExtra("outputY", 360);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
// intent.putExtra("outputFormat",
// Bitmap.CompressFormat.JPEG.toString());
intent.putExtra("noFaceDetection", true); // no face detection
startActivityForResult(intent, 1);
}
2、在onActivityResult()方法中:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
String text;
switch (requestCode) {
case 1:
Uri selectedImage = data.getData();
CutPic(selectedImage);
break;
case 3:// 對圖片進行剪切
if (data != null) {
Bitmap bitmap = data.getParcelableExtra("data");
temps = zoomImage(bitmap, 360, 360);
// 上傳圖片
uploadImg(temps);
}
break;
default:
break;
}
}
}
3、圖片剪切 CutPic(selectedImage);
/**
* 將圖片裁剪到指定大小
*
* @param uri
* @param size
* @param flag
*/
public void CutPic(Uri uri) {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", true);// 設置Intent中的view是可以裁剪的
// 設置寬高比
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 設置裁剪圖片的寬高
intent.putExtra("outputX", 360);
intent.putExtra("outputY", 360);
intent.putExtra("outputFormat", "JPEG");// 圖片格式
// 設置是否返回數據
intent.putExtra("return-data", true);
// 開啟一個帶有返回值的Activity,請求碼為3
startActivityForResult(intent, 3);
}
4、圖片壓縮剪切zoomImage(bitmap, 360, 360);
/***
* 圖片的縮放方法
*
* @param bgimage
* :源圖片資源
* @param newWidth
* :縮放後寬度
* @param newHeight
* :縮放後高度
* @return
*/
public static Bitmap zoomImage(Bitmap bgimage, double newWidth,
double newHeight) {
// 獲取這個圖片的寬和高
float width = bgimage.getWidth();
float height = bgimage.getHeight();
// 創建操作圖片用的matrix對象
Matrix matrix = new Matrix();
// 計算寬高縮放率
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 縮放圖片動作
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, (int) width,
(int) height, matrix, true);
return bitmap;
}
5、上傳圖片文件至服務器uploadImg(bitMaps);
/**
* 上傳圖片
*
* @param bitP
*/
private void uploadImg(final Bitmap bitP) {
// 將Bitmap轉換成字符串
String string = null;
ByteArrayOutputStream bStream = new ByteArrayOutputStream();
bitP.compress(CompressFormat.JPEG, 100, bStream);
byte[] bytes = bStream.toByteArray();
string = Base64.encodeToString(bytes, Base64.DEFAULT);
try {
bStream.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
//string 文件上傳服務器...
}
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Android多線程研究(9)——讀寫鎖
一、什麼是鎖在Java的util.concurrent.locks包下有關於鎖的接口和類如下:先看一段代碼:package com.codeing.snail.test;
Android開發--多線程下載加斷點續傳
文件下載在App應用中也用到很多,一般版本更新時多要用的文件下載來進行處理,以前也有看過很多大神有過該方面的博客,今天我也自己來實踐一下,寫的一般,還請大家多提意見,共同
Android Studio小技巧之多行編輯
Android Studio 大家應該都很熟悉了,但是可能很多人都僅限基本的功能使用,而 Android Studio 非常強大,有很多非常實用卻又鮮為人知的小技巧,熟練
Android Studio導入GitHub上的項目常見問題(以圖片輪播開源項目為實例)
前言:github對開發者而言無疑是個寶藏,但想利用它可不是件簡單的事,用Android studio導入開源項目會遇到各種問題,今天我就以github上的一個圖片輪播項