1、獲取圖片
1、相冊
以隱氏intent的方式打開系統(tǒng)默認的圖庫,需要傳入mimeType
代碼如下:
//打開圖片
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
//Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent, REQUEST_GALLERY);
1、ACTION_PICK、ACTION_GET_CONTENT可以完成相似的功能
2、mimeType:該activity可以處理的文件類型,形式:[type]/[subtype]。 Android MimeType的用途以及所有類型
在onActivityResult中,可以通過以下方法得到Uri:
Uri uri = data.getData();
2、相機
同樣使用隱式的intent,打開系統(tǒng)的相機
1、使用默認的返回路徑
代碼如下:
Intent cameraIntent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent,REQUEST_CAMERA);
所以,在onActivityResult中,可以通過以下方法得到圖片:
Bundle bundle=data.getExtras();
Bitmap bitmap= (Bitmap) bundle.get("data");
imageView.setImageBitmap(bitmap);
但是,camera應用程序,不會將全尺寸的圖片傳遞給主調(diào)程序:系統(tǒng)為了防止應用內(nèi)存占用過大,對于在應用內(nèi)通過相機拍攝的圖片最終返回來的結果進行了壓縮,壓縮后的圖片變得很小,如下:
2、傳遞給camera應用程序,一個路徑
為了得到期望的圖片,可以為camera應用程序,傳遞一個附加值,這個附加值的名稱在MediaStore中指定:EXTRA_OUTPUT,以URI的形式指示捕獲的圖像放置的位置(imgUri)
//傳遞給camera應用程序,一個附加值
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(intent,REQUEST_CAMERA);
- 創(chuàng)建Uri
- 1、使用ContentResolver,因為是添加圖片,用insert
Uri imgUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues()); - 2、 android文件存儲路徑
String imgPath= Environment.getExternalStorageDirectory().getAbsolutePath()+"/_"+System.currentTimeMillis();
Uri uri=Uri.parse(imgPath);
注意:權限,android.permission.WRITE_EXTERNAL_STORAGE
2、裁剪
可以在onActivityResult中調(diào)用裁剪圖片的intent,如下:
Intent cropIntent=new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(srcUri,"image/*");
cropIntent.putExtra("scale", true);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT,imgUri);
startActivityForResult(cropIntent,IMG_CROP);
3、onActivityResult中的回調(diào)
不管是拍照還是通過相冊,總有辦法得到Uri,通過這個Uri就可以取得bitmap或者imgPath
取得bitmap:
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imgUri), null, null);
或者
Bitmap bitmap= MediaStore.Images.Media.getBitmap(getContentResolver(),imgUri);
或者
Bitmap bitmap= BitmapFactory.decodeFile(imgPath, null);
取得imgPath:
/**
* 根據(jù)Uri轉變成真實路徑
*/
public String getRealFilePath(Uri uri) {
String scheme=uri.getScheme();
if(scheme==null || scheme.equals(ContentResolver.SCHEME_FILE)){
String p= uri.getPath();
return p;
}
Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
if (cursor == null) {
return null;
}
if (cursor.moveToFirst()) {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
cursor.close();
}
return null;
}
之后,對得到的bitmap進行壓縮:
4、壓縮
圖片有三種存在形式:硬盤上時是file,網(wǎng)絡傳輸時是stream,內(nèi)存中是stream或bitmap
質(zhì)量壓縮,它其實只能實現(xiàn)對file的影響,你可以把一個file轉成bitmap再轉成file,或者直接將一個bitmap轉成file時,這個最終的file是被壓縮過的,但是中間的bitmap并沒有被壓縮(或者說幾乎沒有被壓縮,我不確定),因為bigmap在內(nèi)存中的大小是按像素計算的,也就是width * height,對于質(zhì)量壓縮,并不會改變圖片的像素,所以就算質(zhì)量被壓縮了,但是bitmap在內(nèi)存的占有率還是沒變小,但你做成file時,它確實變小了;
尺寸壓縮,由于是減小了圖片的像素,所以它直接對bitmap產(chǎn)生了影響,當然最終的file也是相對的變小了;
- 質(zhì)量壓縮
public String qualityCompressImg(Bitmap bitmap, String outPath, int maxSize) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
float option = 100f;
bitmap.compress(Bitmap.CompressFormat.JPEG, (int) option, byteArrayOutputStream);
while (byteArrayOutputStream.toByteArray().length / 1024 > maxSize) {
byteArrayOutputStream.reset();
option *= 0.9f;
if(option<1){
break;
}
bitmap.compress(Bitmap.CompressFormat.JPEG, (int) option, byteArrayOutputStream);
}
try {
FileOutputStream outputStream = new FileOutputStream(outPath);
outputStream.write(byteArrayOutputStream.toByteArray());
outputStream.flush();
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
return outPath;
}
- 尺寸壓縮
public Bitmap ratioBitmap(String path, int viewWidth, int viewHeight) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inJustDecodeBounds = true;
options.inPreferredConfig = Bitmap.Config.RGB_565;
BitmapFactory.decodeFile(path, options);
int h = options.outHeight;
int w = options.outWidth;
int inSampleSize = (int) Math.max(h * 1.0 / viewWidth, w * 1.0 / viewHeight);
if (inSampleSize <= 0) {
inSampleSize = 1;
}
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;
return BitmapFactory.decodeFile(path, options);
}
5、操作Exif信息
為什么在有些手機上拍攝的照片看起來方向不對?
用相機拍攝出來的照片含有EXIF信息,ExifInterface.TAG_ORIENTATION指的就是EXIF中的orientation信息。如果我們忽略orientation信息,直接去獲取圖片的bitmap,得到的結果會旋轉(90°、180°、270°)。所以在onActivityResult方法獲取到照片數(shù)據(jù)后,讀取exif信息,將照片旋轉到正確的方向(Matrix)。
什么是Exif?
Exif是一種圖像文件格式,是在JPEG格式頭插入了照片的信息。通過ExifInterface類可以操作圖片的Exif信息,其中定義了一些字符串的靜態(tài)常量:
TAG_APERTURE:光圈值。
TAG_DATETIME:拍攝時間,取決于設備設置的時間。
TAG_EXPOSURE_TIME:曝光時間。
TAG_FLASH:閃光燈。
TAG_FOCAL_LENGTH:焦距。
TAG_IMAGE_LENGTH:圖片高度。
TAG_IMAGE_WIDTH:圖片寬度。
TAG_ISO:ISO。
TAG_MAKE:設備品牌。
TAG_MODEL:設備型號,整形表示,在ExifInterface中有常量對應表示。
TAG_ORIENTATION:旋轉角度,整形表示,在ExifInterface中有常量對應表示。
使用setAttribute()設置Exif信息,將不會寫入到目標圖片中,只有在改變Exif信息后,調(diào)用saveAttribute()才可以把新的Exif寫入到目標圖片中。
代碼如下:
讀取exif信息:
ExifInterface oldExif=new ExifInterface(pathImage);
Class<ExifInterface> exifInterfaceClass=ExifInterface.class;
Field[] fields=exifInterfaceClass.getFields();
for(int i=0;i<fields.length;i++){
String fieldName = fields[i].getName();
if (!TextUtils.isEmpty(fieldName) && fieldName.startsWith("TAG")) {
String fieldValue = fields[i].get(exifInterfaceClass).toString();
String attribute = oldExif.getAttribute(fieldValue);
L.e("exif",fieldName+"-"+fieldValue+"-"+attribute);
}
}
旋轉圖片:
public String rotateImg(String imgPath, int maxSize) {
Bitmap bitmap=BitmapFactory.decodeFile(imgPath, null);
int rotate=0;
try {
ExifInterface old = new ExifInterface(imgPath);
int orientation=old.getAttributeInt(ExifInterface.TAG_ORIENTATION,0);
if(orientation==ExifInterface.ORIENTATION_ROTATE_90){
rotate=90;
}else if(orientation==ExifInterface.ORIENTATION_ROTATE_270){
rotate=270;
}else if(orientation==ExifInterface.ORIENTATION_ROTATE_180){
rotate=180;
}
} catch (IOException e) {
e.printStackTrace();
}
Matrix matrix=new Matrix();
matrix.setRotate(rotate, bitmap.getWidth()/2,bitmap.getHeight()/2);
Bitmap tempBitmap=Bitmap.createBitmap(bitmap,0,0,bitmap.getWidth(),bitmap.getHeight(),matrix,true);
if(tempBitmap!=bitmap){
bitmap.recycle();
}
return qualityCompressImg(tempBitmap, imgPath, maxSize);
}
如果rotate=0,recycle(),會有問題?
因為 tempBitmap和bitmap可能會是同一個
http://www.cnblogs.com/plokmju/p/android_exif.html
http://blog.csdn.net/berber78/article/details/39778181
http://blog.csdn.net/u012816041/article/details/50602246
6、七牛 上傳圖片
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url=new URL("http://101.201.211.229/zbhq/Home/BabyShow/upToken");
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.connect();
if(connection.getResponseCode()==200){
InputStream inputStream=connection.getInputStream();
ByteArrayOutputStream outputStream=new ByteArrayOutputStream();
byte[] buff=new byte[1024];
int len=0;
while ((len=inputStream.read(buff))!=-1){
outputStream.write(buff,0,len);
}
String jsonString=outputStream.toString();
outputStream.close();
inputStream.close();
JSONObject jsonObject=new JSONObject(jsonString);
final String code=jsonObject.getString("code");
final String uptoken=jsonObject.getString("uptoken");
UploadManager uploadManager=new UploadManager();
String key="babyShow/" +System.currentTimeMillis();
uploadManager.put(uploadPath, key, uptoken, new UpCompletionHandler() {
@Override
public void complete(String key, ResponseInfo info, JSONObject response) {
final String path="http://7xrpiy.com1.z0.glb.clouddn.com/"+key;
text2.setText(code + ":" + path);
new Thread(new Runnable() {
@Override
public void run() {
try {
URL url1=new URL(path);
HttpURLConnection connection1= (HttpURLConnection) url1.openConnection();
if(connection1.getResponseCode()==200){
InputStream inputStream1=connection1.getInputStream();
final Bitmap bitmap=BitmapFactory.decodeStream(inputStream1);
runOnUiThread(new Runnable() {
@Override
public void run() {
imageView2.setImageBitmap(bitmap);
}
});
inputStream1.close();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}, null);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
}
}).start();
代碼:
https://coding.net/u/hongji/p/TakePictureDemo/git
參考:
http://blog.csdn.net/jdsjlzx/article/details/44228935
http://blog.csdn.net/floodingfire/article/details/8144604