标签:android style blog color io os 使用 java ar
注意:
在SD的使用中需要加入以下权限:
<!--在SDCard中创建与删除文件权限 –> <uses-permissioandroid:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <!--往SDCard写入数据权限 --> <uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
思路:
1、在SD卡存在可用的前提下拍摄照片。
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) // 获取SD卡状态,并且值为Environment.MEDIA_MOUNTED时进行照片的拍摄。
2、创建放置图片的目录。
private static final File PHOTO_DIR = new File(Environment.getExternalStorageDirectory() + "/DCIM/Camera"); // 图片的存储目录 PHOTO_DIR.mkdir();//创建目录
3、设置图片存放路径并命名。即图片文件。
File mCurrentPhotoFile;//图片文件 mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName()); // 用当前时间给取得的图片命名 /** * 创建一个新的图片名 */ private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat format = new SimpleDateFormat("‘IMG‘_yyyyMMdd_HHmmss"); return format.format(date) + ".jpg"; }
4、调用照相机,并且为可获得结果的跳转。
private static final int CAMERA_WITH_DATA = 0; // 拍照 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");//跳转到照相 Uri fromFile = Uri.fromFile(mCurrentPhotoFile); // String string = fromFile.toString(); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile)); startActivityForResult(intent, CAMERA_WITH_DATA);
5、重写onActivityResult方法(处理、显示、保存)。
详情看以下源码:
import android.app.Activity; import android.content.Intent; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.net.Uri; import android.os.Bundle; import android.os.Environment; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import com.example.test.R; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * Created by Administrator on 2014/9/9. */ public class GetPic extends Activity { private static final File PHOTO_DIR = new File( Environment.getExternalStorageDirectory() + "/DCIM/Camera"); // 图片的存储目录 File mCurrentPhotoFile;//图片文件 private static final int CAMERA_WITH_DATA = 0; // 拍照 private Integer mStepImg = 0; private static final int SCALE = 4;// 照片缩小比例 private List<Bitmap> mBitmaps = new ArrayList<Bitmap>(); private ImageView mImg_pic1, mImg_pic2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.get_pic); mImg_pic1 = (ImageView) findViewById(R.id.view1); mImg_pic2 = (ImageView) findViewById(R.id.view2); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { getPicFromCapture(); } }); } private void getPicFromCapture() { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {//判断SDCard的状态 PHOTO_DIR.mkdir();//创建目录 mCurrentPhotoFile = new File(PHOTO_DIR, getPhotoFileName()); // 用当前时间给取得的图片命名 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");//跳转到照相 Uri fromFile = Uri.fromFile(mCurrentPhotoFile); // String string = fromFile.toString(); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(mCurrentPhotoFile)); startActivityForResult(intent, CAMERA_WITH_DATA); } else { Log.e("---------", "没有sd卡"); } } /** * 创建一个新的图片名 */ private String getPhotoFileName() { Date date = new Date(System.currentTimeMillis()); SimpleDateFormat format = new SimpleDateFormat("‘IMG‘_yyyyMMdd_HHmmss"); return format.format(date) + ".jpg"; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode != RESULT_OK) return; switch (requestCode) { case CAMERA_WITH_DATA: {// 照相机程序返回的,再次调用图片剪辑程序去修剪图片 mStepImg++; // Bundle extras = data.getExtras(); // final Bitmap bitmap = (Bitmap) extras.get("data"); // 获取图片、缩小、显示、保存 Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoFile.toString());//根据文件全路径获取图片 final ImgModel imgModel = new ImgModel(); imgModel.path = mCurrentPhotoFile.toString(); // imgModel.bitmap = bitmap; Bitmap newBitmap = zoomBitmap(bitmap, bitmap.getWidth() / SCALE, bitmap.getHeight() / SCALE);//缩放图片 bitmap.recycle();// 由于Bitmap内存占用较大,这里需要回收内存,否则会报out of memory异常 mBitmaps.add(bitmap);//??如果用到缩放前图片 mBitmaps.add(newBitmap); mImg_pic1.setScaleType(ImageView.ScaleType.CENTER_CROP); mImg_pic2.setScaleType(ImageView.ScaleType.CENTER_CROP);//按比例扩大图片的size居中显示,使得图片长(宽)等于或大于View的长(宽) // 生成图片显示在界面上 if (mStepImg == 1) { mImg_pic1.setTag(mStepImg); mImg_pic1.setImageBitmap(newBitmap);// mImg_pic2.setVisibility(View.VISIBLE); } else if (mStepImg == 2) { mImg_pic2.setTag(mStepImg); mImg_pic2.setImageBitmap(newBitmap); } // 将处理过的图片显示在界面上,并保存到本地 savePhotoToSDCard(newBitmap, Environment.getExternalStorageDirectory().getAbsolutePath(), String.valueOf(System.currentTimeMillis())); //Environment.getExternalStorageDirectory().getAbsolutePath()是获取外部存储路径的绝对路径,即SD卡的文件路径 //此处的事件是保存事件 break; } } } public Bitmap zoomBitmap(Bitmap bitmap, int width, int height) {//缩小BitMap 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; } /** * 保存图片 */ public void savePhotoToSDCard(Bitmap photoBitmap, String path, String photoName) { if (checkSDCardAvailable()) { File dir = new File(path); if (!dir.exists()) {//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)) { fileOutputStream.flush();//清除 // fileOutputStream.close(); } } } catch (FileNotFoundException e) { photoFile.delete(); e.printStackTrace(); } catch (IOException e) { photoFile.delete(); e.printStackTrace(); } finally { try { fileOutputStream.close();//关闭 } catch (IOException e) { e.printStackTrace(); } } } } /** * 判断SD卡是否可用 * * @return */ public boolean checkSDCardAvailable() { return android.os.Environment.getExternalStorageState().equals( android.os.Environment.MEDIA_MOUNTED); } }
ImgMode.java
/** * Created by Administrator on 2014/9/9. */ public class ImgModel { public String path; }
6、保存图片。
标签:android style blog color io os 使用 java ar
原文地址:http://www.cnblogs.com/ccddy/p/3966533.html