码迷,mamicode.com
首页 > 移动开发 > 详细

Android拍照并保存(对图片进行压缩,分解)

时间:2014-11-06 17:27:37      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:android   拍照   保存图片   压缩   

项目中摘出来的一个小功能模块,有需要的朋友可以借鉴下。


效果图:

bubuko.com,布布扣



具体功能:将拍摄的照片保存到SDcard中


具体需求:

1.图片名保存格式:图片名称_手机型号_随机4位数.jpg

2.将图片分解,压缩,显示

3.写入SDCard卡保存


具体实现:

1.定义布局文件

2.编写业务代码


这里只对调用相机拍照、图片显示、保存  做介绍   并提供源码下载


点击按钮后:

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_getphoto:
                mPhotoName = mETphotoName.getEditableText().toString();
                mModel = mETmodel.getEditableText().toString();
                if(TextUtils.isEmpty(mPhotoName) || TextUtils.isEmpty(mModel)){
                    Toast.makeText(this,"请输入图片名称和型号",0).show();
                    return;
                }
                final String state = Environment.getExternalStorageState();
                if (Environment.MEDIA_MOUNTED.equals(state)) {
                    final Intent takePictureImIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    ContentValues values = new ContentValues();
                    mPhotoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                    takePictureImIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mPhotoUri);
                    startActivityForResult(takePictureImIntent,REQUEST_CODE_TAKE_PICTURE);
                } else {
                    Toast.makeText(this,"请检查内存卡", 0).show();
                }
                break;

            default:
                break;
        }

    }


onActivityResult回调:

/* (non-Javadoc)
     * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {

            case REQUEST_CODE_TAKE_PICTURE:
                // 通过照相获取图片
                if (resultCode == Activity.RESULT_OK) {
                    final Uri uri = mPhotoUri;
                    Log.e("MainActivity",uri+"");
                    if (uri != null) {
                        processPicture(uri);
                    }

                    //提示保存图片
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setTitle("提示");
                    builder.setMessage("是否需要保存图片?");
                    builder.setNegativeButton("取消", null);
                    builder.setPositiveButton("是的", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            String resetPhotoName = mPhotoName+"_"+mModel+"_"+Utils.getRandomString(4);
                            Utils.savePhotoToSDCard(bitmap,Utils.genProjectPath(),resetPhotoName);
                            Toast.makeText(MainActivity.this,"成功--->"+Utils.genProjectPath()+resetPhotoName,0).show();

                            //清空文本框
                            mETphotoName.setText("");
                            mETmodel.setText("");

                            dialog.cancel();
                        }
                    });
                    builder.show();

                }
                break;

            default:
                super.onActivityResult(requestCode, resultCode, data);
                break;
        }
    }


图片显示:

/**
     * 图片显示
     * @param uri
     */
    private void processPicture(Uri uri) {
        final String[] projection = {MediaStore.Images.Media.DATA};
        final Cursor cursor = managedQuery(uri, projection, null, null, null);
        cursor.moveToFirst();
        final int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        String imagePath = cursor.getString(columnIndex);
        Log.e("MainActivity",imagePath);

        final String targetPath = BitmapUtils.toRegularHashCode(imagePath) + ".jpg";
        BitmapUtils.compressBitmap(imagePath, targetPath, 640);  //压缩
        bitmap = BitmapUtils.decodeBitmap(imagePath, 150);       //分解
        mImage.setImageBitmap(bitmap);                           //显示

        //上传服务器
        //final AvatarUrlUpdateRequest request = new AvatarUrlUpdateRequest(REQUEST_ID_AVATAR_URL_UPDATE,mFileName,targetPath,mLoginResult);
        //final LoginRequest request = new LoginRequest(0,"a@a.com","123456");
        //genericDataManager.retrieveData(request, this);

    }



源码下载地址:http://download.csdn.net/detail/gao_chun/8128717

Android拍照并保存(对图片进行压缩,分解)

标签:android   拍照   保存图片   压缩   

原文地址:http://blog.csdn.net/gao_chun/article/details/40864121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!