Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- tools:context="[removed for stackOverflow bc included full name]">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:id="@+id/btn"
- android:layout_gravity="center_horizontal"
- android:layout_marginTop="40dp"
- android:textAppearance="?android:attr/textAppearanceLarge"
- android:text="Add Image" />
- <android.support.v7.widget.RecyclerView
- android:id="@+id/galleryRecyclerView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_centerHorizontal="true" />
- </LinearLayout>
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="wrap_content" android:layout_height="wrap_content">
- <ImageView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_margin="1dp"
- android:id="@+id/imageGalleryView"
- android:layout_centerVertical="true"
- android:layout_centerHorizontal="true"
- />
- </RelativeLayout>
- public class MainActivity extends AppCompatActivity {
- private Button btn;
- private ImageView imageview;
- private static final String IMAGE_DIRECTORY = "/BingoAppGallery";
- private int GALLERY = 1, CAMERA = 2;
- private String mImageFileLocation = "";
- private String galleryLocation = "Bingo Gallery";
- private File BingoGalleryFolder;
- private RecyclerView recycleView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- /******/
- createImageGallery();
- recycleView = (RecyclerView) findViewById(R.id.galleryRecyclerView);
- GridLayoutManager layoutManager = new GridLayoutManager(this,2);
- recycleView.setLayoutManager(layoutManager);
- RecyclerView.Adapter imageAdapter = new ImageAdapter(BingoGalleryFolder);
- recycleView.setAdapter(imageAdapter);
- btn = (Button) findViewById(R.id.btn);
- btn.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- showPictureDialog();
- }
- });
- }
- private void showPictureDialog(){
- AlertDialog.Builder pictureDialog = new AlertDialog.Builder(this);
- pictureDialog.setTitle("Select Action");
- 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:
- takePhotoFromCamera();
- break;
- }
- }
- });
- pictureDialog.show();
- }
- public void choosePhotoFromGallary() {
- Intent galleryIntent = new Intent(Intent.ACTION_PICK,
- android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(galleryIntent, GALLERY);
- }
- private void takePhotoFromCamera() {
- Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
- startActivityForResult(intent, CAMERA);
- }
- @Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- super.onActivityResult(requestCode, resultCode, data);
- if (resultCode == this.RESULT_CANCELED) {
- return;
- }
- if (requestCode == GALLERY) {
- if (data != null) {
- Uri contentURI = data.getData();
- try {
- Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), contentURI);
- String path = saveImage(bitmap);
- Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
- } catch (IOException e) {
- e.printStackTrace();
- Toast.makeText(MainActivity.this, "Failed!", Toast.LENGTH_SHORT).show();
- }
- }
- } else if (requestCode == CAMERA) {
- Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
- saveImage(thumbnail);
- Toast.makeText(MainActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
- }
- RecyclerView.Adapter newImageAdapter = new ImageAdapter(BingoGalleryFolder); //***************//
- recycleView.swapAdapter(newImageAdapter,false);
- }
- public String saveImage(Bitmap myBitmap) {
- ByteArrayOutputStream bytes = new ByteArrayOutputStream();
- myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
- File wallpaperDirectory = new File(
- Environment.getExternalStorageDirectory() + IMAGE_DIRECTORY);
- // have the object build the directory structure, if needed.
- if (!wallpaperDirectory.exists()) {
- wallpaperDirectory.mkdirs();
- }
- try {
- File f = new File(wallpaperDirectory, Calendar.getInstance()
- .getTimeInMillis() + ".jpg");
- f.createNewFile();
- FileOutputStream fo = new FileOutputStream(f);
- fo.write(bytes.toByteArray());
- MediaScannerConnection.scanFile(this,
- new String[]{f.getPath()},
- new String[]{"image/jpeg"}, null);
- fo.close();
- Log.d("TAG", "File Saved::--->" + f.getAbsolutePath());
- return f.getAbsolutePath();
- } catch (IOException e1) {
- e1.printStackTrace();
- }
- return "";
- }
- private void createImageGallery()
- {
- File storageDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
- BingoGalleryFolder = new File(storageDirectory,galleryLocation);
- if(!BingoGalleryFolder.exists())
- BingoGalleryFolder.mkdirs();
- }
- public class ImageAdapter extends RecyclerView.Adapter<ImageAdapter.ViewHolder>
- {
- private File imagesFile;
- public ImageAdapter(File folderFile)
- {
- imagesFile = folderFile;
- }
- @Override
- public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
- {
- View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.gallery_images_relative_layout,parent,false);
- return new ViewHolder(view);
- }
- @Override
- public void onBindViewHolder(ViewHolder holder, int position)
- {
- File imageFile =imagesFile.listFiles()[position];
- Bitmap imageBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
- holder.getImageView().setImageBitmap(imageBitmap);
- }
- @Override
- public int getItemCount()
- {
- return imagesFile.listFiles().length;
- }
- public static class ViewHolder extends RecyclerView.ViewHolder
- {
- private ImageView imageView;
- public ViewHolder(View view)
- {
- super(view);
- imageView = (ImageView) view.findViewById(R.id.imageGalleryView);
- }
- public ImageView getImageView()
- {
- return imageView;
- }
- }
- }
Add Comment
Please, Sign In to add comment