
Introduction:
In my previous article, we have learned How to handle “android.os.FileUriExposedException”, if we have an app that shares files with other apps using an Uri on API 24+. In this article, we will learn “How to use Camera Intent in Android N above devices”.
Creating New Project with Android Studio:
- Open Android Studio and Select Create new project.
- Name the project as your wish and select your activity template.
- Click “finish” button to create new project in Android Studio.
Steps to use Camera API:
- We already know how to open camera from our app using intents. But for Android N & above devices, Google changed the approach for accessing Camera API.
- In this step, we will see how to use Camera Intent for Pre Android N & Android N above devices.
- Open your MainActivity.java file and add a button with click event to open Camera. The following code snippet shows the camera intent.
btnPickCamera.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { // Checking Permission for Android M and above if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_FROM_CAMERA); return; } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) { ContentValues values = new ContentValues(1); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg"); outputFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(captureIntent, PICK_FROM_CAMERA); } else { Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg"); outputFileUri = Uri.fromFile(file); captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(captureIntent, PICK_FROM_CAMERA); } } });
- Here, I have added runtime permission checking for Android M & above devices.
ContentValues values = new ContentValues(1); values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg"); outputFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(captureIntent, PICK_FROM_CAMERA);
- Here, we have created ContentValues and added MIME type as image. Then created an output Uri for the camera api output image.
- Then we have added flags “FLAG_GRANT_READ_URI_PERMISSION” and “FLAG_GRANT_WRITE_URI_PERMISSION” to allow the camera api to take image in Android N & above devices.
- We can retrieve the picked camera image by “onActivityResult” and same like the old approach
Full Code:
The following code shows, How to access image from Gallery, How to take picture from camera & How to open file using intent in Android N & above devices.
public class MainActivity extends AppCompatActivity {
ImageView imgPreview;
TextView imgPath;
Button btnPickCamera;
Button btnPickGallery;
Button btnOpenFile;
Uri outputFileUri;
private static final int PICK_FROM_CAMERA = 1;
private static final int PICK_FROM_GALLERY = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initViews();
initOperations();
}
private void initViews() {
imgPreview = findViewById(R.id.imgPreview);
imgPath = findViewById(R.id.imgPath);
btnPickCamera = findViewById(R.id.btnCapture);
btnPickGallery = findViewById(R.id.btnGallery);
btnOpenFile = findViewById(R.id.btnOpenImg);
}
private void initOperations() {
btnPickGallery.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View view) {
// Checking Permission for Android M and above
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_FROM_GALLERY);
return;
}
Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// Start the Intent
startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
}
});
btnPickCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// Checking Permission for Android M and above
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_FROM_CAMERA);
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
ContentValues values = new ContentValues(1);
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpg");
outputFileUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
captureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(captureIntent, PICK_FROM_CAMERA);
} else {
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(), "MyPhoto.jpg");
outputFileUri = Uri.fromFile(file);
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(captureIntent, PICK_FROM_CAMERA);
}
}
});
btnOpenFile.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
@Override
public void onClick(View v) {
// Checking Permission for Android M and above
if (ActivityCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PICK_FROM_GALLERY);
return;
}
File file = new File(imgPath.getText().toString());
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkURI = FileProvider.getUriForFile(getApplicationContext(), getPackageName() + ".provider", file);
intent.setDataAndType(apkURI, "image/jpg");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
intent.setDataAndType(Uri.fromFile(file), "image/jpg");
}
startActivity(intent);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap;
switch (requestCode) {
case PICK_FROM_CAMERA:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = outputFileUri;
ContentResolver cr = getContentResolver();
getContentResolver().notifyChange(selectedImage, null);
try {
bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
int nh = (int) ( bitmap.getHeight() * (512.0 / bitmap.getWidth()) );
bitmap = Bitmap.createScaledBitmap(bitmap, 512, nh, true);
imgPreview.setImageBitmap(bitmap);
imgPath.setText(outputFileUri.getPath());
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
}
}
break;
case PICK_FROM_GALLERY:
if (resultCode == Activity.RESULT_OK) {
//pick image from gallery
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
// Get the cursor
assert selectedImage != null;
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn,
null, null, null);
// Move to first row
assert cursor != null;
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String imgDecodableString = cursor.getString(columnIndex);
cursor.close();
bitmap = BitmapFactory.decodeFile(imgDecodableString);
imgPreview.setImageBitmap(bitmap);
imgPath.setText(imgDecodableString);
}
break;
}
}
}
Serves24 is Best Geyser repair In Hyderabad We Provided Door Step Repair And Service & Provide best in class. Get 6 months repair guarantee. Book Now just @249/-. at most affordable prices
ReplyDeletegeyser repair services
geyser repair services Hyderabad
geyser repairs
geyser services
geyser repairs Hyderabad
Best geyser repairs
Really Very Good Article
Đặt mua vé máy bay tại Aivivu, tham khảo
ReplyDeletevé máy bay đi Mỹ bao nhiêu tiền
ve may bay tet pacific airlines
đặt vé máy bay đi toronto canada
ve may bay di Phap gia bao nhieu
giá vé máy bay từ Hà Nội đi Anh
xem vé máy bay giá rẻ
vé máy bay đi San Francisco giá rẻ 2021
vé máy bay giá rẻ đi Los Angeles
tour combo phú quốc
combo nha trang 3 ngày 2 đêm
Aivivu chuyên vé máy bay, tham khảo
ReplyDeletevé máy bay đi Mỹ giá bao nhiêu
vé máy bay đi mỹ hãng ana
vé máy bay từ đức về sài gòn
lịch bay hà nội moscow
Snapchat focuses on instant messaging and media sharing, while CapCut is a video editing tool. They serve distinct functions check website.
ReplyDelete