上传图片总结

关于上传图片模糊的问题:

 (一)图片裁剪之后模糊的问题

 在Android中,调用系统相机拍照时,将会接收到返回的图像数据,但是这些图片并不是全尺寸的图像,而是系统给的缩略图,当对拍照的图片进行裁切后显示时,得到的却是模糊的图片。下面针对这个问题提出解决的方法。
首先,我们知道调用系统的裁切是通过
Intent intent = new Intent(“com.android.camera.action.CROP”);

intent.putExtra(“return-data”, true):表示裁剪后返回的数据为Bitmap,是存在内存中的缩略图,效果模糊。获取的方式为,在Activity中的onActivityResult方法中:

Bundle bundle = data.getExtras();
Bitmap bitmap = bundle.getParcelable("data");

为了获取到裁切后的原图,我们选择将剪切的图片保存在本地,然后调用本地的图片,并不直接返回Bitmap.

intent.putExtra("return-data", false);

intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

下面代码实现拍照/剪切并进行显示的:

//保存图片的地址
private String imageName;
//拍照的地址
private Uri tempUri;
//拍照请求码
protected static final int TAKE_PICTURE = 110;
//相册请求码
protected static final int CHOOSE_PICTURE = 120;
//裁剪之后的请求码
private static final int CROP_SMALL_PICTURE = 123;
//拍照
case R.id.camera:
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//拍完照片之后的路径
tempUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), imageName + ".png"));
// 指定照片保存路径(SD卡),imageName + ".png"为一个临时文件,每次拍照后这个图片都会被替换
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
startActivityForResult(openCameraIntent, TAKE_PICTURE);
break;
//相册
case R.id.gallery:
Intent i = new Intent(
Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, CHOOSE_PICTURE);
break;




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
//拍照
case TAKE_PICTURE:
startPhotoZoom(tempUri); // 开始对图片进行裁剪处理
break;
//相册
case CHOOSE_PICTURE:
if (null != data && null != data.getData()) {
startPhotoZoom(data.getData()); // 开始对图片进行裁剪处理
}

break;
//裁剪之后的图片
case CROP_SMALL_PICTURE:
if (tempUri != null) {
photo = decodeUriBitmap(tempUri);
uploadPic(photo);
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}

/**
* 保存图片到本地
*
* @param bitmap
*/
private void uploadPic(Bitmap bitmap) {
iv_image.setImageBitmap(bitmap);
String imagePath = savePhoto(bitmap, Environment.getExternalStorageDirectory().getAbsolutePath(), imageName);
Log.e("imagePath", imagePath);
}

/**
* 保存图片到图库
*
* @param photoBitmap
* @param path
* @param photoName
* @return
*/
public static String savePhoto(Bitmap photoBitmap, String path,
String photoName) {
String localPath = null;
if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED)) {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}

File photoFile = new File(path, photoName + ".png");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(photoFile);
if (photoBitmap != null) {
if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) { // 转换完成
localPath = photoFile.getPath();
fileOutputStream.flush();
}
}
} catch (FileNotFoundException e) {
photoFile.delete();
localPath = null;
e.printStackTrace();
} catch (IOException e) {
photoFile.delete();
localPath = null;
e.printStackTrace();
} finally {
try {
if (fileOutputStream != null) {
fileOutputStream.close();
fileOutputStream = null;
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
return localPath;
}

/**
* 获取裁剪之后的图片
*
* @param uri
* @return
*/
private Bitmap decodeUriBitmap(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
return null;
}
return bitmap;
}

/**
* 裁剪图片方法实现
*
* @param uri
*/
protected void startPhotoZoom(Uri uri) {
tempUri = uri;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
// crop为true是设置在开启的intent中设置显示的view可以剪裁
intent.putExtra("crop", "true");
intent.putExtra("scale", true);
// aspectX aspectY 是宽高的比例
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// outputX,outputY 是剪裁图片的宽高
intent.putExtra("outputX", 800);
intent.putExtra("outputY", 800);
//设置了true的话直接返回bitmap,可能会很占内存
intent.putExtra("return-data", false);
//设置输出的格式
intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
//设置输出的地址
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//不启用人脸识别
intent.putExtra("noFaceDetection", true);
startActivityForResult(intent, CROP_SMALL_PICTURE);
}

<!-- 保存图片权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<!-- 外置存储存取权限 -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

http://git.oschina.net/anan9303/ImageDemo

原文地址:https://www.cnblogs.com/huihuizhang/p/6135694.html