Guest User

Untitled

a guest
Apr 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.17 KB | None | 0 0
  1. protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
  2. super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
  3.  
  4. switch(requestCode) {
  5. case REQ_CODE_PICK_IMAGE:
  6. if(resultCode == RESULT_OK){
  7. Uri selectedImage = imageReturnedIntent.getData();
  8. String[] filePathColumn = {MediaStore.Images.Media.DATA};
  9.  
  10. Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
  11. cursor.moveToFirst();
  12.  
  13. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  14. String filePath = cursor.getString(columnIndex);
  15. cursor.close();
  16.  
  17.  
  18. Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
  19.  
  20. //Now do whatever processing you want to do on it.
  21. }
  22. }
  23. }
  24.  
  25. Intent intent=new Intent();
  26. intent.setType=("image/*");
  27. intent.setAction(Intent.ACTION_GET_CONTENT);
  28. startActivityForResult(Intent.createChooser(intent,"Select Picture"), PICK_IMAGE);
  29. /*Declare PICK_IMAGE globally : private static final int PICK_IMAGE = 1; */
  30.  
  31. protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  32.  
  33. switch (requestCode) {
  34. case PICK_IMAGE:
  35. if (resultCode == Activity.RESULT_OK) {
  36. Uri selectedImageUri = data.getData();
  37. try {
  38. // OI FILE Manager
  39. String filemanagerstring = selectedImageUri.getPath();
  40.  
  41. // MEDIA GALLERY
  42. String selectedImagePath = getPath(selectedImageUri);
  43.  
  44. if (selectedImagePath != null) {
  45. filePath = selectedImagePath;
  46. } else if (filemanagerstring != null) {
  47. filePath = filemanagerstring;
  48. } else {
  49. Toast.makeText(getApplicationContext(), "Unknown path",
  50. Toast.LENGTH_LONG).show();
  51. Log.e("Bitmap", "Unknown path");
  52. }
  53.  
  54. if (filePath != null) {
  55. Log.e("file path ","file path "+filePath);
  56. GlobalValues.camefromtw="true";
  57. decodeFile(filePath);
  58. } else {
  59. bitmap = null;
  60. }
  61. } catch (Exception e) {
  62. Toast.makeText(getApplicationContext(), "Internal error",
  63. Toast.LENGTH_LONG).show();
  64. Log.e(e.getClass().getName(), e.getMessage(), e);
  65. }
  66. }
  67. break;protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  68.  
  69. switch (requestCode) {
  70. case PICK_IMAGE:
  71. Log.e("result code","result code"+resultCode+" result "+Activity.RESULT_OK);
  72. if (resultCode == Activity.RESULT_OK) {
  73. Uri selectedImageUri = data.getData();
  74. try {
  75. // OI FILE Manager
  76. String filemanagerstring = selectedImageUri.getPath();
  77.  
  78. // MEDIA GALLERY
  79. String selectedImagePath = getPath(selectedImageUri);
  80.  
  81. if (selectedImagePath != null) {
  82. filePath = selectedImagePath;
  83. } else if (filemanagerstring != null) {
  84. filePath = filemanagerstring;
  85. } else {
  86. Toast.makeText(getApplicationContext(), "Unknown path",
  87. Toast.LENGTH_LONG).show();
  88. Log.e("Bitmap", "Unknown path");
  89. }
  90.  
  91. if (filePath != null) {
  92.  
  93. decodeFile(filePath);
  94. } else {
  95. bitmap = null;
  96. }
  97. } catch (Exception e) {
  98. Toast.makeText(getApplicationContext(), "Internal error",
  99. Toast.LENGTH_LONG).show();
  100. Log.e(e.getClass().getName(), e.getMessage(), e);
  101. }
  102. }
  103. break;
  104. default:
  105. }
  106.  
  107. public void decodeFile(String filePath) {
  108.  
  109. // Decode image size
  110. BitmapFactory.Options o = new BitmapFactory.Options();
  111. o.inJustDecodeBounds = true;
  112. BitmapFactory.decodeFile(filePath, o);
  113.  
  114. // The new size we want to scale to
  115. final int REQUIRED_SIZE = 1024;
  116.  
  117. // Find the correct scale value. It should be the power of 2.
  118. int width_tmp = o.outWidth, height_tmp = o.outHeight;
  119. int scale = 1;
  120. while (true) {
  121. if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
  122. break;
  123. width_tmp /= 2;
  124. height_tmp /= 2;
  125. scale *= 2;
  126. }
  127.  
  128. // Decode with inSampleSize
  129. BitmapFactory.Options o2 = new BitmapFactory.Options();
  130. o2.inSampleSize = scale;
  131. bitmap = BitmapFactory.decodeFile(filePath, o2);
  132.  
  133. image.setImageBitmap(bitmap);
  134.  
  135. }
  136.  
  137. ImageView profile;
  138. Bitmap bmp;
  139. SharedPreferences sp;
  140. public static final int PERMISSION_REQUEST_CAMERA = 1;
  141. private static final int PICK_FROM_GALLERY = 1;
  142.  
  143. sp=getSharedPreferences("profilePicture",MODE_PRIVATE);
  144. profile=(ImageView)findViewById(R.id.profile);
  145. if(!sp.getString("dp","").equals("")){
  146. byte[] decodedString = Base64.decode(sp.getString("dp", ""), Base64.DEFAULT);
  147. Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
  148. profile.setImageBitmap(decodedByte);
  149. }
  150.  
  151. profile.setOnClickListener(new View.OnClickListener() {
  152. @Override
  153. public void onClick(View v) {
  154. openGallery();
  155. }
  156. });
  157.  
  158. @Override
  159. public void onRequestPermissionsResult(int requestCode, @NonNull String permissions[], @NonNull int[] grantResults)
  160. {
  161. switch (requestCode) {
  162. case PICK_FROM_GALLERY:
  163. // If request is cancelled, the result arrays are empty.
  164. if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
  165. Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  166. startActivityForResult(galleryIntent, PICK_FROM_GALLERY);
  167. } else {
  168. Toast.makeText(ProfileActivity.this,"Not working",Toast.LENGTH_LONG).show();
  169. //do something like displaying a message that he didn`t allow the app to access gallery and you wont be able to let him select from gallery
  170. }
  171. break;
  172. }
  173. }
  174.  
  175. @Override
  176. protected void onActivityResult(int requestCode, int resultCode, Intent data)
  177. {
  178.  
  179. super.onActivityResult(requestCode, resultCode, data);
  180. if (resultCode == RESULT_OK) {
  181. try { // When an Image is picked
  182. if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK
  183. && null != data) {
  184. // Get the Image from data
  185.  
  186. Uri selectedImage = data.getData();
  187. String[] filePathColumn = { MediaStore.Images.Media.DATA };
  188.  
  189. // Get the cursor
  190. Cursor cursor = getContentResolver().query(selectedImage,
  191. filePathColumn, null, null, null);
  192. // Move to first row
  193. cursor.moveToFirst();
  194.  
  195. int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
  196. String imgDecodableString = cursor.getString(columnIndex);
  197. cursor.close();
  198. // Set the Image in ImageView after decoding the String
  199. bmp = BitmapFactory
  200. .decodeFile(imgDecodableString);
  201. profile.setImageBitmap(bmp);
  202. ByteArrayOutputStream stream = new ByteArrayOutputStream();
  203.  
  204. bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
  205. byte[] byteArray = stream.toByteArray();
  206. String encodedImage = Base64.encodeToString(byteArray, Base64.DEFAULT);
  207. sp.edit().putString("dp", encodedImage).commit();
  208. } else {
  209. Toast.makeText(this, "You haven't picked Image",
  210. Toast.LENGTH_LONG).show();
  211. }
  212. } catch (Exception e) {
  213. e.printStackTrace();
  214. Toast.makeText(ProfileActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
  215. }
  216.  
  217. }else {
  218. Toast.makeText(ProfileActivity.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
  219. }
  220. }
Add Comment
Please, Sign In to add comment