Android调取系统相册和相机照片设置到ImageView并上传到服务器

时间:2022-09-18 22:40:51

网上的这类答案很多,不同的手机也会出现不同的情况,自己每次做这个功能都要掉进去很多次坑,分享给大家,也自己做一个笔记

话不多先看下效果

Android调取系统相册和相机照片设置到ImageView并上传到服务器 Android调取系统相册和相机照片设置到ImageView并上传到服务器 Android调取系统相册和相机照片设置到ImageView并上传到服务器 Android调取系统相册和相机照片设置到ImageView并上传到服务器 Android调取系统相册和相机照片设置到ImageView并上传到服务器 Android调取系统相册和相机照片设置到ImageView并上传到服务器 Android调取系统相册和相机照片设置到ImageView并上传到服务器 Android调取系统相册和相机照片设置到ImageView并上传到服务器 Android调取系统相册和相机照片设置到ImageView并上传到服务器



代码开始:

首先定义一个 PopupWindow

private PopupWindow pop = null;

然后在监听里面这么写:

pop = new PopupWindow(ModifiedDataActivity.this);
View view = getLayoutInflater().inflate(R.layout.item_popupwindows, null);//加载布局

pop.setWidth(ViewGroup.LayoutParams.MATCH_PARENT); //设置PopupWindow 一些参数
pop.setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setBackgroundDrawable(new BitmapDrawable());
pop.setFocusable(true);
pop.setOutsideTouchable(true);
pop.setContentView(view);
pop.showAtLocation(view, Gravity.BOTTOM, 0, 0);

RelativeLayout parent = (RelativeLayout) view.findViewById(R.id.parent); //最外层布局
Button bt1 = (Button) view.findViewById(R.id.item_popupwindows_camera);
Button bt2 = (Button) view.findViewById(R.id.item_popupwindows_Photo);
Button bt3 = (Button) view.findViewById(R.id.item_popupwindows_cancel);
parent.setOnClickListener(new View.OnClickListener() { // 设置点击最外层布局关闭PopupWindow
@Override
public void onClick(View v) {
pop.dismiss();

}
});
bt1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {//相机
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用android自带的照相机
Uri photoUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
startActivityForResult(intent, 1);
pop.dismiss();
}
});
bt2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {//相册
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//调用android的图库
startActivityForResult(i, 2);
pop.dismiss();
}
});
bt3.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
pop.dismiss();
}
});

//这个方法是拍照或者选择照片之后系统自动回调的方法

@Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {

if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1) { // 拍照
Bundle extras = data.getExtras();
Bitmap photoBit = (Bitmap) extras.get("data");
Bitmap option = BitmapOption.bitmapOption(photoBit, 5);
iv_aprove_head_portrait.setImageBitmap(option);
saveBitmap2file(option, "0001.jpg");
final File file = new File("/sdcard/" + "0001.jpg");

Log.e("TAG", "file333333333333333 " + file.getName());
//开始联网上传的操作

} else if (requestCode == 2) { // 相册
try {
Uri uri = data.getData();
String[] pojo = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(uri, pojo, null, null, null);
if (cursor != null) {
ContentResolver cr = this.getContentResolver();
int colunm_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(colunm_index);
final File file = new File(path);
Bitmap bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
Bitmap option = BitmapOption.bitmapOption(bitmap, 5);
iv_aprove_head_portrait.setImageBitmap(option);//设置为头像的背景 Log.e("TAG", "fiels11111 " + file.getName());

                     //开始联网上传的操作

}
} catch (Exception e) {

}
}
}
}


//将图片保存到本地

    static boolean saveBitmap2file(Bitmap bmp, String filename) {
Bitmap.CompressFormat format = Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;
try {
stream = new FileOutputStream("/sdcard/" + filename);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return bmp.compress(format, quality, stream);
}


//压缩图片的工具类,上面有用到  

bitmapOption中的size 代表压缩的级别

public class BitmapOption {

private static final BitmapOption bitmapOption = new BitmapOption();

private BitmapOption() {
}

public static BitmapOption getBitmapOption() {
return bitmapOption;
}


public static Bitmap bitmapOption(Bitmap image, int size) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 85, out);
float zoom = (float) Math.sqrt(size * 1024 / (float) out.toByteArray().length);
Matrix matrix = new Matrix();
matrix.setScale(zoom, zoom);
Bitmap result = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
out.reset();
result.compress(Bitmap.CompressFormat.JPEG, 85, out);
while (out.toByteArray().length > size * 1024) {
System.out.println(out.toByteArray().length);
matrix.setScale(0.9f, 0.9f);
result = Bitmap.createBitmap(result, 0, 0, result.getWidth(), result.getHeight(), matrix, true);
out.reset();
result.compress(Bitmap.CompressFormat.JPEG, 85, out);
}
return result;
}


}