安卓调用系统摄像头拍照并保存到本地

最近在写新项目,需求里面需要用到摄像头拍照,并且把排下来的照片放进ImageView中显示,为了后续控件的调用,自然想到要存储在本地,安卓很人性的提供了好多数据存储的方式,在本项目里我选择了存储在手机内存卡上面,调用摄像头拍照思路很简单,利用安卓系统内部的Intent对象传递参数,进行方法回调达到目的,大致源码如下

/**
 * Created by 谢栋 on 2016/8/14.
 */
public class UpGoods extends Fragment {
    private View mView;
    private ImageView goodsImageView;
    private EditText titleEditText;
    private EditText contentEditText;
    private EditText nameEditText;
    private EditText phoneEditText;
    private Button commitButton;
    private GoodsBenn goodsBenn;

    private Bitmap mBitmap;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        mView = inflater.inflate(R.layout.loadesgoods, null);
        //初始化Bmob的SDK
        Bmob.initialize(getActivity(), "df25a6c6a79479d11a60f2e89c68b467");
        //初始化控件
        initView();

        return mView;
    }

    private void initView() {
        goodsImageView = (ImageView) mView.findViewById(R.id.iv_goods);
        titleEditText = (EditText) mView.findViewById(R.id.ed_title);
        contentEditText = (EditText) mView.findViewById(R.id.ed_desc);
        nameEditText = (EditText) mView.findViewById(R.id.ed_name);
        phoneEditText = (EditText) mView.findViewById(R.id.ed_phone);
        commitButton = (Button) mView.findViewById(R.id.commit);

        goodsImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                takePhoto();
            }
        });

        //给按钮绑定监听事件
        commitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                uploadGoods();
            }
        });
    }

    /**
     * 上传会员用户的商品详情到数据库
     */
    private void uploadGoods() {

        String name = nameEditText.getText().toString();
        String phone = phoneEditText.getText().toString();
        String title = titleEditText.getText().toString();
        String content = contentEditText.getText().toString();
        Bitmap bitmap = mBitmap;

        goodsBenn = new GoodsBenn();
        goodsBenn.setName(name);
        goodsBenn.setPhone(phone);
        goodsBenn.setGoodsTiltle(title);
        goodsBenn.setGoodsContent(content);
//        goodsBenn.setGoodsBitmap(bitmap);


        //上传图片文件并且返回图片的url
        String picPath = "/sdcard/qyb/Goods.jpg";
        final BmobFile bmobFile = new BmobFile(new File(picPath));
        bmobFile.uploadblock(new UploadFileListener() {

            @Override
            public void done(BmobException e) {
                if (e == null) {
                    //bmobFile.getFileUrl()--返回的上传文件的完整地址
                    Log.i("上传文件成功:", bmobFile.getFileUrl().toString());
                    //得到商品存储的url地址之后,代码商品属性加载加载完成,然后把各项数据存进数据库
                    goodsBenn.setGoodsUrl(bmobFile.getFileUrl() + "");
                    goodsBenn.save(new SaveListener<String>() {
                        @Override
                        public void done(String s, BmobException e) {
                            if (e == null) {
                                Toast.makeText(getActivity(), "上传成功", Toast.LENGTH_SHORT).show();
                            } else {
                                Log.i("xie", e.toString());
                            }

                        }
                    });
                } else {
                    Log.i("上传文件失败:", e.getMessage().toString());
                }

            }


            @Override
            public void onProgress(Integer value) {
                // 返回的上传进度(百分比)
            }
        });



    }

    //调用摄像机拍照并存储在指定位置
    private void takePhoto() {
        // TODO Auto-generated method stub
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, 1);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            String sdStatus = Environment.getExternalStorageState();
            if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用
                Log.i("TestFile",
                        "SD card is not avaiable/writeable right now.");
                return;
            }

            String name = "Goods" + ".jpg";
            Toast.makeText(getActivity(), name, Toast.LENGTH_LONG).show();
            Bundle bundle = data.getExtras();
            mBitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式

            FileOutputStream b = null;
            File file = new File("/sdcard/qyb/");
            file.mkdirs();// 创建文件夹
            String fileName = "/sdcard/qyb/" + name;

            try {
                b = new FileOutputStream(fileName);
                mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    b.flush();
                    b.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                goodsImageView.setImageBitmap(mBitmap);// 将图片显示在ImageView里
            } catch (Exception e) {
                Log.e("error", e.getMessage());
            }

        }
    }


}


布局文件也很简单,项目要上传商品信息,一个ImageView,几个editText,加上一个Button,具体代码如下

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
   android:layout_gravity="center"
    >
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center"
        android:layout_gravity="center"
        >
        <ImageView
            android:id="@+id/iv_goods"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:src="@drawable/gesture"
            />
        <EditText
            android:id="@+id/ed_title"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_width="fill_parent"
            android:padding="5dp"
            style="@style/edittextStyle"
            android:layout_height="wrap_content"
            android:hint="请输入您商品的名称"/>
        <EditText
            android:padding="5dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请简短描叙下您的商品"
            style="@style/edittextStyle"

            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:id="@+id/ed_desc"
            />
        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="您的称呼"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:id="@+id/ed_name"
                />
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="联系方式"
                android:layout_marginLeft="5dp"
                android:layout_marginRight="5dp"
                android:id="@+id/ed_phone"
                />
            </LinearLayout>

        <Button
            android:id="@+id/commit"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="提交商品"
            style="@style/btnStyle"
            />
        </LinearLayout>

</LinearLayout>


最后别忘了加权限


 

 <!-- 在SDCard中创建与删除文件权限 -->
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    <!-- 往SDCard写入数据权限 -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-feature android:name = "android.hardware.camera" />
    <uses-feature android:name = "android.hardware.camera.autofocus" />


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值