Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CameraActivity extends AppCompatActivity {
- Integer REQUEST_CAMERA =1, GALLERY_KITKAT_INTENT_CALLED=0;
- private static final int REQUEST_READ_PERMISSION = 100;
- private Uri capturedImageUri;
- public static String selectedImagePath;
- private Bitmap bitmap;
- public ImageView capturedPhoto;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_camera);
- SelectImage();
- capturedPhoto = (ImageView) findViewById(R.id.cameraPhoto);
- }
- private void SelectImage()
- {
- final CharSequence[] items ={"Camera","Gallery","Cancel"};
- AlertDialog.Builder builder = new AlertDialog.Builder(CameraActivity.this);
- builder.setTitle("Add Image");
- builder.setItems(items, new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialogInterface, int i) {
- if (items[i].equals("Camera")) {
- Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- startActivityForResult(intent, REQUEST_CAMERA);
- } else if (items[i].equals("Gallery")) {
- Intent intent2 = new Intent(Intent.ACTION_OPEN_DOCUMENT);
- intent2.addCategory(Intent.CATEGORY_OPENABLE);
- intent2.setType("image/*");
- startActivityForResult(intent2, GALLERY_KITKAT_INTENT_CALLED);
- } else if (items[i].equals("Cancel")) {
- dialogInterface.dismiss();
- }
- }
- });
- builder.show();
- }
- @Override
- public void onActivityResult(int requestCode,int resultCode, Intent data)
- {
- super.onActivityResult(requestCode,resultCode,data);
- if(requestCode == Activity.RESULT_OK && requestCode == REQUEST_CAMERA)
- {
- Bundle extras = data.getExtras();
- Bitmap imageBitmap = (Bitmap)extras.get("data");
- capturedPhoto.setImageBitmap(imageBitmap);
- //Uri tempUri = getCapturedImageUri(getApplicationContext(),imageBitmap);
- //GlobalVariables.filepath = getRealPathFromURIPath(selectedImagePath);
- String result = data.toURI();
- capturedImageUri = data.getData();
- try{
- if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
- ActivityCompat.requestPermissions(CameraActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_READ_PERMISSION);
- }
- else{
- selectedImagePath = getRealPathFromURIPath(capturedImageUri,CameraActivity.this);
- bitmap=MediaStore.Images.Media.getBitmap(getContentResolver(),capturedImageUri);
- capturedPhoto.setImageBitmap(bitmap);
- GlobalVariables.filepath = selectedImagePath;
- setContentView(new SomeView(CameraActivity.this));
- }
- }
- catch (IOException e)
- {
- e.printStackTrace();
- }
- }
- if (requestCode == GALLERY_KITKAT_INTENT_CALLED && resultCode == RESULT_OK && null != data )
- {
- if (requestCode == GALLERY_KITKAT_INTENT_CALLED) {
- String filepath2 = "";
- Uri originalUri = null;
- originalUri = data.getData();
- final int takeFlags = data.getFlags()
- & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
- // Check for the freshest data.
- getContentResolver().takePersistableUriPermission(originalUri,
- takeFlags);
- filepath2 = getPath(originalUri);
- if (filepath2.toString() != null) {
- // LoadPicture(filepath);
- GlobalVariables.filepath = filepath2;
- // cropImageView.setVisibility(View.VISIBLE);
- setContentView(new SomeView(CameraActivity.this));
- }
- }
- }
- }
- @SuppressLint("NewApi")
- private String getPath(Uri uri) {
- if (uri == null) {
- return null;
- }
- String[] projection = {MediaStore.Images.Media.DATA};
- Cursor cursor;
- if (Build.VERSION.SDK_INT > 19) {
- // Will return "image:x*"
- String wholeID = DocumentsContract.getDocumentId(uri);
- // Split at colon, use second item in the array
- String id = wholeID.split(":")[1];
- // where id is equal to
- String sel = MediaStore.Images.Media._ID + "=?";
- cursor = getContentResolver().query(
- MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
- sel, new String[]{id}, null);
- } else {
- cursor = getContentResolver().query(uri, projection, null, null,
- null);
- }
- String path = null;
- try {
- int column_index = cursor
- .getColumnIndex(MediaStore.Images.Media.DATA);
- cursor.moveToFirst();
- path = cursor.getString(column_index).toString();
- cursor.close();
- } catch (NullPointerException e) {
- }
- return path;
- }
- private String getRealPathFromURIPath(Uri contentURI, Activity activity) {
- Cursor cursor = activity.getContentResolver().query(contentURI, null, null, null, null);
- if (cursor == null) {
- return contentURI.getPath();
- } else {
- cursor.moveToFirst();
- int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
- return cursor.getString(idx);
- }
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:app="http://schemas.android.com/apk/res-auto"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="com.mbdp.w.areametric.CameraActivity">
- <ImageView
- android:id="@+id/cameraPhoto"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:layout_alignParentEnd="true"
- android:layout_centerVertical="true" />
- </RelativeLayout>
Add Comment
Please, Sign In to add comment