Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.84 KB | None | 0 0
  1. package com.example.mihovil.digitalnomad.fragments;
  2.  
  3.  
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.content.Intent;
  7. import android.graphics.Bitmap;
  8. import android.net.Uri;
  9. import android.os.Bundle;
  10. import android.provider.MediaStore;
  11. import android.support.annotation.Nullable;
  12. import android.support.design.widget.FloatingActionButton;
  13. import android.support.v4.app.Fragment;
  14. import android.support.v7.widget.LinearLayoutManager;
  15. import android.support.v7.widget.RecyclerView;
  16. import android.view.LayoutInflater;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19. import android.widget.Toast;
  20.  
  21. import com.example.mihovil.digitalnomad.Interface.OnPicturesRecived;
  22. import com.example.mihovil.digitalnomad.R;
  23. import com.example.mihovil.digitalnomad.controller.GalleryAdapter;
  24. import com.example.mihovil.digitalnomad.files.GetImage;
  25. import com.example.webservice.interfaces.Review;
  26. import com.example.webservice.interfaces.ServiceResponse;
  27. import com.example.webservice.interfaces.WebServiceCaller;
  28. import com.example.webservice.interfaces.interfaces.OnServiceFinished;
  29.  
  30. import java.io.IOException;
  31. import java.util.ArrayList;
  32. import java.util.List;
  33.  
  34. import static android.app.Activity.RESULT_OK;
  35.  
  36. /**
  37. * A simple {@link Fragment} subclass.
  38. */
  39. public class ShowWorkspaceGallery extends Fragment implements OnPicturesRecived, DialogInterface.OnClickListener, OnServiceFinished{
  40.  
  41. List<Review> reviews;
  42. private RecyclerView rv;
  43. View view;
  44. Bundle valueBundle;
  45. FloatingActionButton fab;
  46. ArrayList<String> urls;
  47. private static final int PICK_IMAGE = 989;
  48. ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
  49. String newBitmap;
  50. Bitmap workspaceBitmap;
  51.  
  52. public ShowWorkspaceGallery() {
  53. // Required empty public constructor
  54. }
  55.  
  56.  
  57. @Override
  58. public View onCreateView(LayoutInflater inflater, ViewGroup container,
  59. Bundle savedInstanceState) {
  60. // Inflate the layout for this fragment
  61. return inflater.inflate(R.layout.activity_recycler_view, container, false);
  62. }
  63.  
  64. public void onActivityCreated(@Nullable Bundle savedInstanceState){
  65. super.onActivityCreated(savedInstanceState);
  66. view = getView();
  67. rv = (RecyclerView) view.findViewById(R.id.rv);
  68. LinearLayoutManager llm = new LinearLayoutManager(getActivity());
  69. rv.setLayoutManager(llm);
  70. rv.setHasFixedSize(true);
  71. urls = new ArrayList<String>();
  72. urls = getArguments().getStringArrayList("urls");
  73. GetImage getImage = new GetImage(urls, this);
  74. getImage.execute();
  75. fab = (FloatingActionButton) view.findViewById(R.id.add_new_fab);
  76. fab.setOnClickListener(new View.OnClickListener() {
  77. public void onClick(View v) {
  78. DialogInterface.OnClickListener dialogClickListener = ShowWorkspaceGallery.this;
  79.  
  80. AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
  81. builder.setMessage("Select image from gallery?").setPositiveButton("Yes", dialogClickListener)
  82. .setNegativeButton("No", dialogClickListener).show();
  83. }
  84. });
  85. }
  86. @Override
  87. public void picturesReceived(Bitmap[] bitmap) {
  88. for(int i = 0; i<bitmap.length; i++){
  89. bitmaps.add(bitmap[i]);
  90. }
  91. System.out.println("size: " + bitmap.length);
  92. GalleryAdapter ga = new GalleryAdapter(bitmap);
  93. rv.setAdapter(ga);
  94. }
  95.  
  96.  
  97.  
  98. @Override
  99. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  100. super.onActivityResult(requestCode, resultCode, data);
  101.  
  102. if (requestCode == PICK_IMAGE && resultCode == RESULT_OK) {
  103. if (data == null) {
  104. Toast.makeText(getContext(), "No image selected.", Toast.LENGTH_LONG).show();
  105. } else {
  106. Uri selectedImageUri = data.getData();
  107. try {
  108. workspaceBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImageUri);
  109. newBitmap = GetImage.getEncoded64ImageStringFromBitmap(workspaceBitmap);
  110.  
  111.  
  112. WebServiceCaller wsc = new WebServiceCaller(ShowWorkspaceGallery.this);
  113. wsc.uploadWorkspaceImage(getArguments().getString("id"),newBitmap);
  114. } catch (IOException e) {
  115. e.printStackTrace();
  116. }
  117. }
  118. } else {
  119. Toast.makeText(getContext(), "No image selected.", Toast.LENGTH_LONG).show();
  120. }
  121. }
  122.  
  123. @Override
  124. public void onClick(DialogInterface dialogInterface, int i) {
  125. switch (i) {
  126. case DialogInterface.BUTTON_POSITIVE:
  127. Intent intent = new Intent();
  128. intent.setType("image/*");
  129. intent.setAction(Intent.ACTION_GET_CONTENT);
  130. startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
  131. break;
  132.  
  133. case DialogInterface.BUTTON_NEGATIVE:
  134. Toast.makeText(getContext(), "Request canceled", Toast.LENGTH_SHORT).show();
  135. break;
  136. }
  137. }
  138.  
  139. @Override
  140. public void onServiceDone(Object response) {
  141. ServiceResponse isSuccess = (ServiceResponse) response;
  142. if (isSuccess.isPostoji()) {
  143. Toast.makeText(getContext(), "Image uploaded to server", Toast.LENGTH_LONG).show();
  144. }
  145. bitmaps.add(workspaceBitmap);
  146. Bitmap[] bitmapArray = new Bitmap[bitmaps.size()];
  147. for(int i=0; i<bitmaps.size(); i++){
  148. bitmapArray[i] = bitmaps.get(i);
  149. }
  150. GalleryAdapter ga = new GalleryAdapter(bitmapArray);
  151. }
  152.  
  153. @Override
  154. public void onServiceFail(Object message) {
  155.  
  156. }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement