Skip to content

Instantly share code, notes, and snippets.

@root-ansh
Created June 3, 2019 01:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save root-ansh/d310b7a0cd07d38eb3cb93a94519bf1f to your computer and use it in GitHub Desktop.
Save root-ansh/d310b7a0cd07d38eb3cb93a94519bf1f to your computer and use it in GitHub Desktop.
Delegation to system ui camera
public class MainActivity extends AppCompatActivity {
private static final int RQ_CAPTURE_IMG_CODE_HDPIC = 101;
private static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider";
private static final String TAG = "$1477$";
Uri tmpPhotoURI;
ImageView ivTemp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivTemp = findViewById(R.id.iv_tmpcheck);
Button btCallCameraHdPic = findViewById(R.id.bt_call_camera_hd_pic);
btCallCameraHdPic.setOnClickListener(v -> callCameraForHdPic());
}
private void callCameraForHdPic() {
//todo : add code for runtimePermissionCheck here//
File tempImageFile = createImageFile();// step 2
if (tempImageFile != null) {
Log.e(TAG, "callCameraForHdPic: temp image file is not null");
tmpPhotoURI = FileProvider.getUriForFile(this, AUTHORITY, tempImageFile);
//^--step3
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, tmpPhotoURI); //step4
if (i.resolveActivity(getPackageManager()) != null) {
startActivityForResult(i, RQ_CAPTURE_IMG_CODE_HDPIC); //step4
} else {
Log.e(TAG, "callCameraForHdPic: couldn't strt cmra activity");
}
} else {
Log.e(TAG, "callCameraForHdPic: temp image file is null ");
}
}
private File createImageFile() {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = null;
try {
image = File.createTempFile(imageFileName, ".jpg", storageDir);
Log.e(TAG, "createImageFile: succesfully created image file");
} catch (IOException e) {
Log.e("$1457$", "createImageFile: error happenned during temp file creation ");
e.printStackTrace();
}
return image;
}
private void openCameraX1() {
startActivity(new Intent(this,Camera1Activity.class));
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == RQ_CAPTURE_IMG_CODE_HDPIC && resultCode == RESULT_OK) {
//if (data != null && data.getExtras() != null) { ...} //not needed to check data
Log.e(TAG, "onActivityResultCodeForHdPic: result recieved from camera activity");
Bitmap b= getBitmapFromUri(this,tmpPhotoURI);
onBitmapRecieved(b);
}
}
private static Bitmap getBitmapFromUri(Context context, Uri photoURI) {
// super cool decoding algorithm at https://stackoverflow.com/a/31720143/7500651 //
....
return rotatedAndDecodedImg;
}
private void onBitmapRecieved(Bitmap imageBitmap) {
ivTemp.setImageBitmap(imageBitmap);
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="x.y.z.camera1">
<uses-feature
android:name="android.hardware.camera.any"
android:required="true" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
....
>
<activity android:name=".MainActivity">
...
</activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="in.curioustools.camera1.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images" path="Android/data/in.curioustools.camera1/files/Pictures" />
</paths>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment