|
1
2
|
Intent openCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(openCameraIntent, TAKE_PICTURE); |
|
1
|
Uri imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"image.jpg")); |
|
1
|
openCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); |
|
1
2
3
4
5
6
7
8
9
10
11
|
/**拍照获取相片**/ private void doTakePhoto() { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); //调用系统相机 Uri imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"image.jpg")); //指定照片保存路径(SD卡),image.jpg为一个临时文件,每次拍照后这个图片都会被替换 intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //直接使用,没有缩小 startActivityForResult(intent, PHOTO_WITH_CAMERA); //用户点击了从相机获取 } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case TAKE_PICTURE: //将保存在本地的图片取出并缩小后显示在界面上 Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory()+"/image.jpg"); Bitmap newBitmap = zoomBitmap(bitmap, bitmap.getWidth() / SCALE, bitmap.getHeight() / SCALE); //由于Bitmap内存占用较大,这里需要回收内存,否则会报out of memory异常 bitmap.recycle(); //将处理过的图片显示在界面上,并保存到本地 iv_image.setImageBitmap(newBitmap); savePhotoToSDCard(newBitmap, Environment.getExternalStorageDirectory().getAbsolutePath(), String.valueOf(System.currentTimeMillis())); break; default: break; } } } |
|
1
2
3
|
Intent openAlbumIntent = new Intent(Intent.ACTION_GET_CONTENT); openAlbumIntent.setType("image/*"); startActivityForResult(openAlbumIntent, CHOOSE_PICTURE); |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case CHOOSE_PICTURE: ContentResolver resolver = getContentResolver(); //照片的原始资源地址 Uri originalUri = data.getData(); try { //使用ContentProvider通过URI获取原始图片 Bitmap photo = MediaStore.Images.Media.getBitmap(resolver, originalUri); if (photo != null) { //为防止原始图片过大导致内存溢出,这里先缩小原图显示,然后释放原始Bitmap占用的内存 Bitmap smallBitmap = zoomBitmap(photo, photo.getWidth() / SCALE, photo.getHeight() / SCALE); //释放原始图片占用的内存,防止out of memory异常发生 photo.recycle(); iv_image.setImageBitmap(smallBitmap); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } break; default: break; } } } |
|
1
2
3
4
5
6
7
8
9
10
11
|
/** 缩放Bitmap图片 **/ public Bitmap zoomBitmap(Bitmap bitmap, int width, int height) { int w = bitmap.getWidth(); int h = bitmap.getHeight(); Matrix matrix = new Matrix(); float scaleWidth = ((float) width / w); float scaleHeight = ((float) height / h); matrix.postScale(scaleWidth, scaleHeight);// 利用矩阵进行缩放不会造成内存溢出 Bitmap newbmp = Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true); return newbmp; } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
/**从相册获取图片**/ private Intent doPickPhotoFromGallery() { Intent intent = new Intent(); intent.setType("image/*"); // 开启Pictures画面Type设定为image intent.setAction(Intent.ACTION_GET_CONTENT); //使用Intent.ACTION_GET_CONTENT这个Action //实现对图片的裁剪,必须要设置图片的属性和大小 intent.setType("image/*"); //获取任意图片类型 intent.putExtra("crop", "true"); //滑动选中图片区域 intent.putExtra("aspectX", 1); //裁剪框比例1:1 intent.putExtra("aspectY", 1); intent.putExtra("outputX", 300); //输出图片大小 intent.putExtra("outputY", 300); intent.putExtra("return-data", true); //有返回值 return intent; } |
|
1
2
|
Intent intent2 = doPickPhotoFromGallery();startActivityForResult(intent2, PHOTO_WITH_DATA); |
|
1
2
3
4
5
6
7
8
9
|
/** 为图片创建不同的名称用于保存,避免覆盖 **/public static String createFileName() { String fileName = ""; Date date = new Date(System.currentTimeMillis()); // 系统当前时间 SimpleDateFormat dateFormat = new SimpleDateFormat( "'IMG'_yyyyMMdd_HHmmss"); fileName = dateFormat.format(date) + ".jpg"; return fileName;} |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
/**Save image to the SD card**/ public static void savePhotoToSDCard(String path, String photoName, Bitmap photoBitmap) { 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); //在指定路径下创建文件 FileOutputStream fileOutputStream = null; try { fileOutputStream = new FileOutputStream(photoFile); if (photoBitmap != null) { if (photoBitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream)) { fileOutputStream.flush(); } } } catch (FileNotFoundException e) { photoFile.delete(); e.printStackTrace(); } catch (IOException e) { photoFile.delete(); e.printStackTrace(); } finally { try { fileOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } |
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
/** * 插入图片列表适配器 * @author ZHF * */public class ImagesListAdapter extends BaseAdapter { private Context context; private List<String> imagesList; //各个图片的路径 public ImagesListAdapter(Context context, List<String> imagesList) { this.context = context; this.imagesList = imagesList; } /**得到总的数量**/ @Override public int getCount() { // TODO Auto-generated method stub return imagesList.size(); } /**根据ListView位置返回View**/ @Override public Object getItem(int position) { return imagesList.get(position); //返回当前选中的item图片的路径 } /**根据ListView位置得到List中的ID**/ @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; //返回当前选中项的Id } /**根据位置得到View对象**/ @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.newwrite_image_item, null); } ImageView img = (ImageView) convertView.findViewById(R.id.newwrite_et_content_image); //图片列表项 if(!imagesList.get(position).equals("")) {//没有图片 Bitmap tempBitmap = BitmapFactory.decodeFile(imagesList.get(position));//根据路径显示对应的图片 Bitmap newBitmap = new ImageManager(context).zoomBitmap(tempBitmap, tempBitmap.getWidth(), tempBitmap.getHeight() / 3); img.setImageBitmap(newBitmap);//对应的行上显示对应的图片 } return convertView; }} |
424

被折叠的 条评论
为什么被折叠?



