Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(i, SELECT_IMAGE);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK || data == null) {
return;
}
//select an image
if (requestCode == SELECT_IMAGE) {
//get image path from uri
String imagePath = getImagePath(data.getData());
return;
}
}
private String getImagePath(Uri selectedImage) {
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imagePath = cursor.getString(columnIndex);
cursor.close();
System.out.println("image path:" + imagePath);
return imagePath;
}
//select image via system gallery, crop and save the new image file.
public void selectImageAndCrop(View view) {
//After cropping, the image file will be stored here!
cacheFile = imageCacheFolder + File.separator + "cache_" + System.currentTimeMillis() + ".jpg";
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
intent.putExtra("crop", "true");
//width:height
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("output", Uri.fromFile(new File(cacheFile)));
intent.putExtra("outputFormat", "JPEG");
startActivityForResult(Intent.createChooser(intent, "Choose Image"), SELECT_IMAGE_CROP);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_OK) {
return;
}
//select an image and crop
if (requestCode == SELECT_IMAGE_CROP) {
//compress the original image to save memory.
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inSampleSize = 4;
imageView.setImageBitmap(BitmapFactory.decodeFile(cacheFile, opt));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有