public class ImagePicker extends AppCompatActivity {
private Button btn;
private int GALLERY = 1, CAMERA = 2;
private static final String IMAGE_DIRECTORY = "/Cerebrum";
public RecyclerView recyclerView;
public GalleryAdapter galleryAdapter;
// private CameraAdapter cameraAdapter;
List<ImageData> list = new ArrayList<>();
GridLayoutManager gridLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_picker);
requestMultiplePermissions();
btn = findViewById(R.id.btnAddImages);
recyclerView = (RecyclerView) findViewById(R.id.ivrecycler);
gridLayoutManager = new GridLayoutManager(this, 3);
recyclerView.setLayoutManager(gridLayoutManager);
galleryAdapter = new GalleryAdapter(this, list);
recyclerView.setAdapter(galleryAdapter);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showPictureDialog();
}
});
}
private void showPictureDialog() {
AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
pictureDialog.setTitle("Add Photo");
String[] pictureDialogItems = {
"Select photo from gallery",
"Capture photo from camera"};
pictureDialog.setItems(pictureDialogItems,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
choosePhotoFromGallary();
break;
case 1:
try {
takePhotoFromCamera();
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
});
pictureDialog.show();
}
private void takePhotoFromCamera() throws IOException {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, CAMERA);
}
}
}
private void choosePhotoFromGallary() {
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), GALLERY);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
// When an Image is picked
if (requestCode == GALLERY && resultCode == RESULT_OK
&& null != data) {
if (data.getData() != null) {
String timeStamp = new SimpleDateFormat("dd/MM_HH:mm",
Locale.getDefault()).format(new Date());
Uri singleImage = data.getData();
String imgname = singleImage.getLastPathSegment();
ImageData img = new ImageData(singleImage, timeStamp, imgname);
list.add(img);
GalleryAdapter galleryAdapter;
galleryAdapter = new GalleryAdapter(this, list);
recyclerView.setAdapter(galleryAdapter);
galleryAdapter.notifyDataSetChanged();
} else {
if (data.getClipData() != null) {
ClipData mClipData = data.getClipData();
for (int i = 0; i < mClipData.getItemCount(); i++) {
ClipData.Item item = mClipData.getItemAt(i);
Uri multipleimages = item.getUri();
String imgsname = multipleimages.getLastPathSegment();
String timeStamp = new SimpleDateFormat("dd/MM_HH:mm",
Locale.getDefault()).format(new Date());
ImageData img = new ImageData(multipleimages, timeStamp, imgsname);
list.add(img);
galleryAdapter.notifyDataSetChanged();
}
Log.v("LOG_TAG", "Selected Images" + list.size());
}
}
} else if (requestCode == CAMERA && resultCode == RESULT_OK) {
//
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(currentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
String timeStamp = new SimpleDateFormat("dd/MM_HH:mm",
Locale.getDefault()).format(new Date());
String camname = contentUri.getLastPathSegment();
// Bitmap cameraimage = (Bitmap) data.getExtras().get("data");
ImageData img = new ImageData(contentUri,timeStamp,camname);
list.add(img);
// GalleryAdapter galleryAdapter;
// galleryAdapter = new GalleryAdapter(this, list);
// recyclerView.setAdapter(galleryAdapter);
galleryAdapter.notifyDataSetChanged();
// Bitmap photo = (Bitmap) data.getExtras().get("data");
// ByteArrayOutputStream bytes = new ByteArrayOutputStream();
// photo.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
// mArrayUri.add(cameraimage);
}
} catch (Exception e) {
Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG)
.show();
super.onActivityResult(requestCode, resultCode, data);
}
}
String currentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
currentPhotoPath = image.getAbsolutePath();
return image;
}
private void requestMultiplePermissions () {
Dexter.withActivity(this)
.withPermissions(
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE)
.withListener(new MultiplePermissionsListener() {
@Override
public void onPermissionsChecked(MultiplePermissionsReport report) {
// check if all permissions are granted
if (report.areAllPermissionsGranted()) {
}
// check for permanent denial of any permission
if (report.isAnyPermissionPermanentlyDenied()) {
//openSettingsDialog();
}
}
@Override
public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) {
token.continuePermissionRequest();
}
}).
withErrorListener(new PermissionRequestErrorListener() {
@Override
public void onError(DexterError error) {
Toast.makeText(getApplicationContext(), "Some Error! ", Toast.LENGTH_SHORT).show();
}
})
.onSameThread()
.check();
}
}
Comments
Post a Comment