Guest User

Untitled

a guest
Nov 20th, 2017
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.65 KB | None | 0 0
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. tools:context="[removed for stackOverflow bc included full name]">
  7.  
  8. <Button
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:id="@+id/btn"
  12. android:layout_gravity="center_horizontal"
  13. android:layout_marginTop="40dp"
  14. android:textAppearance="?android:attr/textAppearanceLarge"
  15. android:text="Add Image" />
  16.  
  17. <android.support.v7.widget.RecyclerView
  18. android:id="@+id/galleryRecyclerView"
  19. android:layout_width="wrap_content"
  20. android:layout_height="wrap_content"
  21. android:layout_centerHorizontal="true" />
  22. </LinearLayout>
  23.  
  24. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  25. android:layout_width="wrap_content" android:layout_height="wrap_content">
  26. <ImageView
  27. android:layout_width="wrap_content"
  28. android:layout_height="wrap_content"
  29. android:layout_margin="1dp"
  30. android:id="@+id/imageGalleryView"
  31. android:layout_centerVertical="true"
  32. android:layout_centerHorizontal="true"
  33. />
  34. </RelativeLayout>
  35.  
  36. public class MainActivity extends AppCompatActivity {
  37.  
  38. private Button btn;
  39. private ImageView imageview;
  40. private static final String IMAGE_DIRECTORY = "/BingoAppGallery";
  41. private int GALLERY = 1, CAMERA = 2;
  42.  
  43.  
  44.  
  45. private String mImageFileLocation = "";
  46. private String galleryLocation = "Bingo Gallery";
  47. private File BingoGalleryFolder;
  48. private RecyclerView recycleView;
  49.  
  50.  
  51.  
  52. @Override
  53. protected void onCreate(Bundle savedInstanceState) {
  54. super.onCreate(savedInstanceState);
  55. setContentView(R.layout.activity_main);
  56. /******/
  57.  
  58. createImageGallery();
  59. recycleView = (RecyclerView) findViewById(R.id.galleryRecyclerView);
  60. GridLayoutManager layoutManager = new GridLayoutManager(this,2);
  61. recycleView.setLayoutManager(layoutManager);
  62. RecyclerView.Adapter imageAdapter = new ImageAdapter(BingoGalleryFolder);
  63. recycleView.setAdapter(imageAdapter);
  64.  
  65.  
  66.  
  67. btn = (Button) findViewById(R.id.btn);
  68. btn.setOnClickListener(new View.OnClickListener() {
  69. @Override
  70. public void onClick(View v) {
  71. showPictureDialog();
  72. }
  73. });
  74.  
  75. }
  76.  
  77.  
  78. private void showPictureDialog(){
  79. AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
  80. pictureDialog.setTitle("Select Action");
  81. String[] pictureDialogItems = {
  82. "Select photo from gallery",
  83. "Capture photo from camera" };
  84. pictureDialog.setItems(pictureDialogItems,
  85. new DialogInterface.OnClickListener() {
  86. @Override
  87. public void onClick(DialogInterface dialog, int which) {
  88. switch (which) {
  89. case 0:
  90. choosePhotoFromGallary();
  91. break;
  92. case 1:
  93. takePhotoFromCamera();
  94. break;
  95. }
  96. }
  97. });
  98. pictureDialog.show();
  99. }
  100.  
  101. public void choosePhotoFromGallary() {
  102. Intent galleryIntent = new Intent(Intent.ACTION_PICK,
  103. android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
  104.  
  105. startActivityForResult(galleryIntent, GALLERY);
  106. }
  107.  
  108. private void takePhotoFromCamera() {
  109. Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
  110. startActivityForResult(intent, CAMERA);
  111. }
  112.  
  113. @Override
  114. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  115.  
  116. super.onActivityResult(requestCode, resultCode, data);
  117. if (resultCode == this.RESULT_CANCELED) {
  118. return;
  119. }
  120. if (requestCode == GALLERY) {
  121. if (data != null) {
  122. Uri contentURI = data.getData();
  123. try {
  124. Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
  125. String path = saveImage(bitmap);
  126. Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
  127.  
  128.  
  129. } catch (IOException e) {
  130. e.printStackTrace();
  131. Toast.makeText(MainActivity.this, "Failed!", Toast.LENGTH_SHORT).show();
  132. }
  133. }
  134.  
  135. } else if (requestCode == CAMERA) {
  136. Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
  137. saveImage(thumbnail);
  138. Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
  139. }
  140. RecyclerView.Adapter newImageAdapter = new ImageAdapter(BingoGalleryFolder); //***************//
  141. recycleView.swapAdapter(newImageAdapter,false);
  142. }
  143.  
  144. public String saveImage(Bitmap myBitmap) {
  145. ByteArrayOutputStream bytes = new ByteArrayOutputStream();
  146. myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
  147. File wallpaperDirectory = new File(
  148. Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
  149. // have the object build the directory structure, if needed.
  150. if (!wallpaperDirectory.exists()) {
  151. wallpaperDirectory.mkdirs();
  152. }
  153.  
  154. try {
  155. File f = new File(wallpaperDirectory, Calendar.getInstance()
  156. .getTimeInMillis() + ".jpg");
  157. f.createNewFile();
  158. FileOutputStream fo = new FileOutputStream(f);
  159. fo.write(bytes.toByteArray());
  160. MediaScannerConnection.scanFile(this,
  161. new String[]{f.getPath()},
  162. new String[]{"image/jpeg"}, null);
  163. fo.close();
  164. Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
  165.  
  166. return f.getAbsolutePath();
  167. } catch (IOException e1) {
  168. e1.printStackTrace();
  169. }
  170. return "";
  171. }
  172.  
  173.  
  174.  
  175. private void createImageGallery()
  176. {
  177. File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  178. BingoGalleryFolder = new File(storageDirectory,galleryLocation);
  179. if(!BingoGalleryFolder.exists())
  180. BingoGalleryFolder.mkdirs();
  181.  
  182. }
  183.  
  184. public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder>
  185. {
  186. private File imagesFile;
  187.  
  188. public ImageAdapter(File folderFile)
  189. {
  190. imagesFile = folderFile;
  191. }
  192. @Override
  193. public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
  194. {
  195. View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_images_relative_layout,parent,false);
  196. return new ViewHolder(view);
  197. }
  198. @Override
  199. public void onBindViewHolder(ViewHolder holder, int position)
  200. {
  201. File imageFile =imagesFile.listFiles()[position];
  202. Bitmap imageBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
  203. holder.getImageView().setImageBitmap(imageBitmap);
  204.  
  205.  
  206. }
  207. @Override
  208. public int getItemCount()
  209. {
  210. return imagesFile.listFiles().length;
  211. }
  212.  
  213. public static class ViewHolder extends RecyclerView.ViewHolder
  214. {
  215. private ImageView imageView;
  216.  
  217. public ViewHolder(View view)
  218. {
  219. super(view);
  220. imageView = (ImageView) view.findViewById(R.id.imageGalleryView);
  221.  
  222. }
  223. public ImageView getImageView()
  224. {
  225. return imageView;
  226. }
  227. }
  228. }
Add Comment
Please, Sign In to add comment