Guest User

Untitled

a guest
Jan 19th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.43 KB | None | 0 0
  1. public class CameraActivity extends AppCompatActivity {
  2.  
  3. Integer REQUEST_CAMERA =1, GALLERY_KITKAT_INTENT_CALLED=0;
  4. private static final int REQUEST_READ_PERMISSION = 100;
  5. private Uri capturedImageUri;
  6. public static String selectedImagePath;
  7. private Bitmap bitmap;
  8. public ImageView capturedPhoto;
  9.  
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_camera);
  15.  
  16. SelectImage();
  17. capturedPhoto = (ImageView) findViewById(R.id.cameraPhoto);
  18. }
  19.  
  20. private void SelectImage()
  21. {
  22. final CharSequence[] items ={"Camera","Gallery","Cancel"};
  23.  
  24. AlertDialog.Builder builder = new AlertDialog.Builder(CameraActivity.this);
  25. builder.setTitle("Add Image");
  26. builder.setItems(items, new DialogInterface.OnClickListener() {
  27. @Override
  28. public void onClick(DialogInterface dialogInterface, int i) {
  29. if (items[i].equals("Camera")) {
  30. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  31. startActivityForResult(intent, REQUEST_CAMERA);
  32. } else if (items[i].equals("Gallery")) {
  33. Intent intent2 = new Intent(Intent.ACTION_OPEN_DOCUMENT);
  34. intent2.addCategory(Intent.CATEGORY_OPENABLE);
  35.  
  36. intent2.setType("image/*");
  37. startActivityForResult(intent2, GALLERY_KITKAT_INTENT_CALLED);
  38.  
  39. } else if (items[i].equals("Cancel")) {
  40. dialogInterface.dismiss();
  41. }
  42. }
  43. });
  44. builder.show();
  45. }
  46. @Override
  47. public void onActivityResult(int requestCode,int resultCode, Intent data)
  48. {
  49. super.onActivityResult(requestCode,resultCode,data);
  50.  
  51. if(requestCode == Activity.RESULT_OK && requestCode == REQUEST_CAMERA)
  52. {
  53.  
  54. Bundle extras = data.getExtras();
  55. Bitmap imageBitmap = (Bitmap)extras.get("data");
  56. capturedPhoto.setImageBitmap(imageBitmap);
  57. //Uri tempUri = getCapturedImageUri(getApplicationContext(),imageBitmap);
  58. //GlobalVariables.filepath = getRealPathFromURIPath(selectedImagePath);
  59.  
  60.  
  61. String result = data.toURI();
  62. capturedImageUri = data.getData();
  63.  
  64. try{
  65. if(ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){
  66. ActivityCompat.requestPermissions(CameraActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_READ_PERMISSION);
  67. }
  68. else{
  69. selectedImagePath = getRealPathFromURIPath(capturedImageUri,CameraActivity.this);
  70. bitmap=MediaStore.Images.Media.getBitmap(getContentResolver(),capturedImageUri);
  71. capturedPhoto.setImageBitmap(bitmap);
  72. GlobalVariables.filepath = selectedImagePath;
  73. setContentView(new SomeView(CameraActivity.this));
  74. }
  75. }
  76. catch (IOException e)
  77. {
  78. e.printStackTrace();
  79. }
  80.  
  81.  
  82. }
  83.  
  84. if (requestCode == GALLERY_KITKAT_INTENT_CALLED && resultCode == RESULT_OK && null != data )
  85. {
  86. if (requestCode == GALLERY_KITKAT_INTENT_CALLED) {
  87.  
  88. String filepath2 = "";
  89. Uri originalUri = null;
  90. originalUri = data.getData();
  91.  
  92. final int takeFlags = data.getFlags()
  93. & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
  94. // Check for the freshest data.
  95. getContentResolver().takePersistableUriPermission(originalUri,
  96. takeFlags);
  97. filepath2 = getPath(originalUri);
  98.  
  99. if (filepath2.toString() != null) {
  100. // LoadPicture(filepath);
  101. GlobalVariables.filepath = filepath2;
  102. // cropImageView.setVisibility(View.VISIBLE);
  103. setContentView(new SomeView(CameraActivity.this));
  104.  
  105. }
  106. }
  107. }
  108. }
  109.  
  110. @SuppressLint("NewApi")
  111. private String getPath(Uri uri) {
  112. if (uri == null) {
  113. return null;
  114. }
  115.  
  116. String[] projection = {MediaStore.Images.Media.DATA};
  117.  
  118. Cursor cursor;
  119. if (Build.VERSION.SDK_INT > 19) {
  120. // Will return "image:x*"
  121. String wholeID = DocumentsContract.getDocumentId(uri);
  122. // Split at colon, use second item in the array
  123. String id = wholeID.split(":")[1];
  124. // where id is equal to
  125. String sel = MediaStore.Images.Media._ID + "=?";
  126.  
  127. cursor = getContentResolver().query(
  128. MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
  129. sel, new String[]{id}, null);
  130. } else {
  131. cursor = getContentResolver().query(uri, projection, null, null,
  132. null);
  133. }
  134. String path = null;
  135. try {
  136. int column_index = cursor
  137. .getColumnIndex(MediaStore.Images.Media.DATA);
  138. cursor.moveToFirst();
  139. path = cursor.getString(column_index).toString();
  140. cursor.close();
  141. } catch (NullPointerException e) {
  142.  
  143. }
  144. return path;
  145. }
  146.  
  147.  
  148.  
  149. private String getRealPathFromURIPath(Uri contentURI, Activity activity) {
  150. Cursor cursor = activity.getContentResolver().query(contentURI, null, null, null, null);
  151. if (cursor == null) {
  152. return contentURI.getPath();
  153. } else {
  154. cursor.moveToFirst();
  155. int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
  156. return cursor.getString(idx);
  157. }
  158. }
  159. }
  160.  
  161. <?xml version="1.0" encoding="utf-8"?>
  162. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  163. xmlns:app="http://schemas.android.com/apk/res-auto"
  164. xmlns:tools="http://schemas.android.com/tools"
  165. android:layout_width="match_parent"
  166. android:layout_height="match_parent"
  167. tools:context="com.mbdp.w.areametric.CameraActivity">
  168.  
  169. <ImageView
  170. android:id="@+id/cameraPhoto"
  171. android:layout_width="match_parent"
  172. android:layout_height="match_parent"
  173. android:layout_alignParentEnd="true"
  174. android:layout_centerVertical="true" />
  175.  
  176. </RelativeLayout>
Add Comment
Please, Sign In to add comment