android Continuous Photography

Keywords: Mobile Android FileProvider xml encoding

android Photography

  • Layout: Use Recycler View to slide horizontally.
  • II. Photography and System Adaptation
  • 1. Dynamic Judgment Authority
  • 2. FileProvider adaptation
  • <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="Package name.FileProvider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/rc_file_path" />
    </provider>

    xml file

  • <?xml version="1.0" encoding="utf-8"?>
    <paths>
        <external-path
            name="test"
            path="Android/data/com/test" />
        <external-path
            name="external_storage_root"
            path="." />
    </paths>

    Custom path path path

  • 3, take pictures

private void startDeviceCamera() {
    Intent mIntent = new Intent();
    mIntent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
    setSavePath();
    deviceFile = new File(saveFile, System.currentTimeMillis() + ".jpg");
    Uri uri = null;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (checkPermission()) {
            requestPermission();
        } else {
            mIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                uri = FileProvider.getUriForFile(this, "Package name.FileProvider", deviceFile);
            } else {
                uri = Uri.fromFile(deviceFile);
            }
        }
    } else {
        //Converting Uri directly below 6.0
        uri = Uri.fromFile(deviceFile);
    }
    mIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
    startActivityForResult(mIntent, 101);
}

private void setSavePath() {
    saveFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/test"); // Custom Path
    if (!saveFile.exists()) {
        saveFile.mkdirs();
    }
}

Setting up data

 

Bean class declares the image as Object type

private Object imgPath;

Add data to List

AuthDeviceBean deviceBean = new AuthDeviceBean();
deviceBean.setImgPath(R.mipmap.ic_apply_add);
authDeviceList.add(deviceBean);

Setting Click Events for Adapter

authDeviceAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
    @Override
    public void onItemClick(View view, int position) {
        if (position == authDeviceList.size() - 1) {
                startDeviceCamera();
        }
    }
});


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == 101) {
            if (deviceFile.exists() && deviceFile.isFile()) {
                String imgPath = deviceFile.getAbsolutePath();
                AuthDeviceBean authDeviceBean = new AuthDeviceBean();
                authDeviceBean.setImgPath(imgPath);
                authDeviceList.add(authDeviceList.size(), authDeviceBean);
                authDeviceAdapter.notifyDataSetChanged();
            }
        }
    }
}

Four, problems

At this time, the function of continuous photography is preliminarily completed. Two problems were found in operation.

Question 1: After taking the first picture, "+" appears in front of the picture.

Solution: In the onActivityResult() method, add data to List

authDeviceList.add(authDeviceList.size(), authDeviceBean);
// Change to
authDeviceList.add(authDeviceList.size() - 1, authDeviceBean);
//Note: By adding (), the length of the List is subtracted by 1.

Question 2: There is no limit on the number of photos taken.

Solution: In the adapter click event, judge.

if (position == authDeviceList.size() - 1) {
                startDeviceCamera();
        }
// For example: 3 consecutive photographs. List subscript processing.
if (position == authDeviceList.size() - 1) {
    if (authDeviceList.size() <= 3) {
        startDeviceCamera();
    } else {
        Toast.makeText(mContext, "Up to 3 pieces.", Toast.LENGTH_SHORT).show();
    }
}

Continuous photography function is completed.

Posted by Clarkey_Boy on Wed, 23 Jan 2019 11:48:13 -0800