1. 程式人生 > >Android中開啟系統相機連續拍多張圖片,並得到圖片地址

Android中開啟系統相機連續拍多張圖片,並得到圖片地址

1、開啟系統相機:
                Intent intent = new Intent();
                intent.putExtra("return-data", false);
                intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE_SECURE);
                startActivityForResult(intent, 100);

2、拍完多張照片回來:
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 100) {
//            Log.v("tag_2", "" + data);
        }
    }

3、查詢資料庫得到最新的一張圖片:
    public String getLastPhotoPath() {
        String photoPath = "";
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        ContentResolver contentResolver = getContentResolver();
        String[] projection = {MediaStore.Images.Media._ID,
                MediaStore.Images.Media.DISPLAY_NAME,
                MediaStore.Images.Media.DATA};
        String selection = MediaStore.Images.Media.MIME_TYPE + "=?";
        String[] selectionArgs = {"image/jpeg"};
        String sortOrder = MediaStore.Images.Media.DATE_MODIFIED + " desc limit 1";
        Cursor cursor = contentResolver.query(uri, projection, selection, selectionArgs, sortOrder);
        if (cursor != null) {
            cursor.moveToFirst();
            do {
                photoPath = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
            } while (cursor.moveToNext());
            cursor.close();
        }
        return photoPath;
    }

4、註冊資料庫監聽:

        mContentObserver = new CameraObserver(new Handler());
        getContentResolver().registerContentObserver(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, true, mContentObserver);
    public class CameraObserver extends ContentObserver {
        public CameraObserver(Handler handler) {
            super(handler);
        }

        @Override
        public void onChange(boolean selfChange, Uri uri) {
            ImageView imageView = (ImageView) findViewById(R.id.image);
            String photoPath = getLastPhotoPath();
            Picasso.with(MainActivity.this).load(new File(photoPath)).centerCrop().resize(Utils.dip2px(200f), Utils.dip2px(200f)).into(imageView);
        }
    }


如果你覺得幫到了你,請給作者打賞一口飯吃: