android拍照保存全尺寸图片

来源:互联网 发布:先创网络 编辑:程序博客网 时间:2024/06/11 02:08

这种保存图片的好处:在有限的内存下,管理许多全尺寸的图片会很棘手。如果发现应用在展示了少量图片后消耗了所有内存,我们可以通过缩放图片到目标视图尺寸,之后再载入到内存中的方法,来显著降低内存的使用。

demo已解决的问题:在onCreate()得到控件的宽度和长度,

在onCreate()方法中调用

ViewTreeObserver vto = image.getViewTreeObserver();vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {    public boolean onPreDraw() {        targetH = image.getMeasuredHeight();        targetW = image.getMeasuredWidth();        return true;    }});

如果希望照片对我们的应用而言是私有的,那么可以使用getExternalFilesDir()提供的目录

如果希望照片对所有应用共享,那么可以使用getExternalStoragePublicDirectory()提供的目录

首先加一个写的权限

<manifest...>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    ...
</manifest>

按钮点击

button.setOnClickListener(new View.OnClickListener() {    @Override    public void onClick(View view) {        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        //判断是否有相机        if (intent.resolveActivity(getPackageManager()) != null) {            File photoFIle=null;            try {                photoFIle = createImageFile();            } catch (Exception e) {                e.printStackTrace();            }            if (photoFIle != null) {                //怎么将拍好的照片保存在sd卡中?               /*               首先在sd卡中创建一个空的.jpg文件,然后获取创建好的.jpguri,               接着通过intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFIle));               MediaStore.EXTRA_OUTPUT这个表示照相完成后保存图片在uri这个.jpg文件中                */                intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFIle));                gallerAddPic();                startActivityForResult(intent,REQUEST_IMAGES);            }        }    }});
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {    if (requestCode == REQUEST_IMAGES && resultCode == RESULT_OK) {        setPic();     /*   Bundle bundle=data.getExtras();        Bitmap bitmap = (Bitmap) bundle.get("data");        image.setImageBitmap(bitmap);*/    }}
private void setPic(){    Log.d("test", targetH+";;;");    BitmapFactory.Options bmOptions=new BitmapFactory.Options();    bmOptions.inJustDecodeBounds=true;//设置inJustDecodeBoundstrue后,decodeFile并不分配空间,但可计算出原始图片的长度和宽度    //bmOptions指向mCurrentPhotoPaht路径中的图片    BitmapFactory.decodeFile(mCurrentPhotoPaht, bmOptions);    //之所以phtotW=0是因为mCurrentPhotoPaht的路径出错,找不到拍照后保存下的图片    float photoW=bmOptions.outHeight;    float photoH=bmOptions.outHeight;    Log.d("test", "photoW" + photoW);    float scaleFactor = Math.min(photoW / targetW, photoH / targetH);//设置缩放比例    bmOptions.inJustDecodeBounds=false;//    bmOptions.inSampleSize=(int) scaleFactor;    bmOptions.inPurgeable=true;//inPurgeable:设置为True,表示系统内存不足时可以被回 收    Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPaht, bmOptions);    image.setImageBitmap(bitmap);}//触发系统的Media Scanner,将我们的照片添加到Media Provider的数据库中,就是将照片添加到相册,拍照后你就可以在相册中看到private void gallerAddPic(){    Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);    Log.d("test", mCurrentPhotoPaht + ":gallerAddPic");    File f = new File(mCurrentPhotoPaht);    Uri contentUri = Uri.fromFile(f);    mediaScanIntent.setData(contentUri);    this.sendBroadcast(mediaScanIntent);}private File createImageFile() throws IOException{    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());    String imageFileName = "JPEG_" + timeStamp + "_";    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);    File image = File.createTempFile(imageFileName, ".jpg", storageDir);    mCurrentPhotoPaht=image.getAbsolutePath();    Log.d("test", mCurrentPhotoPaht);    return image;}
//保存路径长度,宽度,防止重建Activity时长度,宽度,mCureentPhotoPaht出错
@Overrideprotected void onSaveInstanceState(Bundle outState) {    outState.putString("path",mCurrentPhotoPaht);    outState.putInt("w",targetW);    outState.putInt("h",targetH);    Log.d("test", "onSaveInstance"+mCurrentPhotoPaht);    super.onSaveInstanceState(outState);}@Overrideprotected void onRestoreInstanceState(Bundle savedInstanceState) {    mCurrentPhotoPaht = savedInstanceState.getString("path");    targetH = savedInstanceState.getInt("h");    targetW = savedInstanceState.getInt("w");    Log.d("test", "onresotre:" + mCurrentPhotoPaht);    super.onRestoreInstanceState(savedInstanceState);}


0 0