Guest User

Untitled

a guest
Nov 19th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.24 KB | None | 0 0
  1. public class ImageGallery extends AppCompatActivity {
  2. public static ArrayList<Model_images> al_images = new ArrayList<>();
  3. boolean boolean_folder;
  4. Adapter_PhotosFolder obj_adapter;
  5. GridView gv_folder;
  6. private static final int REQUEST_PERMISSIONS = 100;
  7. int int_position;
  8.  
  9. @Override
  10. protected void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.activity_image_gallery);
  13. gv_folder = (GridView) findViewById(android.R.id.list);
  14. obj_adapter = new Adapter_PhotosFolder(this,al_images,int_position);
  15. gv_folder.setAdapter(obj_adapter);
  16.  
  17. final ImageButton button1 = (ImageButton) findViewById(R.id.button1);
  18. final ImageButton button2 = (ImageButton) findViewById(R.id.button2);
  19. final ImageButton button3 = (ImageButton) findViewById(R.id.button3);
  20. final ImageButton button4 = (ImageButton) findViewById(R.id.button4);
  21. final ImageButton button5 = (ImageButton) findViewById(R.id.button5);
  22. button1.setVisibility(View.GONE);
  23. button2.setVisibility(View.GONE);
  24. button3.setVisibility(View.GONE);
  25. button4.setVisibility(View.GONE);
  26. button5.setVisibility(View.GONE);
  27.  
  28. gv_folder.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  29. @Override
  30. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  31. Intent intent = new Intent(getApplicationContext(), PhotosActivity.class);
  32. intent.putExtra("value",i);
  33. startActivity(intent);
  34. }
  35. });
  36.  
  37. gv_folder.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  38. @Override
  39. public boolean onItemLongClick(AdapterView<?> adapterView, View view,final int i, long l) {
  40. for (int j = 0; j < adapterView.getChildCount(); j++)
  41. adapterView.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
  42.  
  43. // change the background color of the selected element
  44. view.setBackgroundColor(Color.LTGRAY);
  45. button1.setVisibility(View.VISIBLE);
  46. button2.setVisibility(View.VISIBLE);
  47. button3.setVisibility(View.VISIBLE);
  48. button4.setVisibility(View.VISIBLE);
  49. button5.setVisibility(View.VISIBLE);
  50. button3.setOnClickListener(
  51. new View.OnClickListener() {
  52. public void onClick(View view) {
  53. AlertDialog.Builder builder1 = new AlertDialog.Builder(ImageGallery.this);
  54. builder1.setMessage("Are you sure you want to delete it ?");
  55. builder1.setCancelable(true);
  56.  
  57. builder1.setPositiveButton(
  58. "Yes",
  59. new DialogInterface.OnClickListener() {
  60. public void onClick(DialogInterface dialog, int id) {
  61. File file = new File(al_images.get(int_position).getAl_imagepath().get(i));
  62. file.delete();
  63. al_images.remove(i);
  64. obj_adapter.notifyDataSetChanged();
  65. finish();
  66. }
  67. });
  68.  
  69. builder1.setNegativeButton(
  70. "No",
  71. new DialogInterface.OnClickListener() {
  72. public void onClick(DialogInterface dialog, int id) {
  73. dialog.cancel();
  74. }
  75. });
  76.  
  77. AlertDialog alert11 = builder1.create();
  78. alert11.show();
  79. }
  80. });
  81.  
  82. return true;
  83. }
  84. });
  85.  
  86. if ((ContextCompat.checkSelfPermission(getApplicationContext(),
  87. Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) && (ContextCompat.checkSelfPermission(getApplicationContext(),
  88. Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)) {
  89. if ((ActivityCompat.shouldShowRequestPermissionRationale(ImageGallery.this,
  90. Manifest.permission.WRITE_EXTERNAL_STORAGE)) && (ActivityCompat.shouldShowRequestPermissionRationale( ImageGallery.this,
  91. Manifest.permission.READ_EXTERNAL_STORAGE))) {
  92.  
  93. } else {
  94. ActivityCompat.requestPermissions( ImageGallery.this,
  95. new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
  96. REQUEST_PERMISSIONS);
  97. }
  98. }else {
  99. Log.e("Else","Else");
  100. fn_imagespath();
  101. }
  102. }
  103.  
  104. public ArrayList<Model_images> fn_imagespath() {
  105. al_images.clear();
  106.  
  107. int int_position = 0;
  108. Uri uri;
  109. Cursor cursor;
  110. int column_index_data, column_index_folder_name;
  111.  
  112. String absolutePathOfImage;
  113. uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  114.  
  115. String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
  116.  
  117. final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
  118. cursor = getApplicationContext().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
  119.  
  120. column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
  121. column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
  122. while (cursor.moveToNext()) {
  123. absolutePathOfImage = cursor.getString(column_index_data);
  124. Log.e("Column", absolutePathOfImage);
  125. Log.e("Folder", cursor.getString(column_index_folder_name));
  126.  
  127. for (int i = 0; i < al_images.size(); i++) {
  128. if (al_images.get(i).getStr_folder().equals(cursor.getString(column_index_folder_name))) {
  129. boolean_folder = true;
  130. int_position = i;
  131. break;
  132. } else {
  133. boolean_folder = false;
  134. }
  135. }
  136.  
  137.  
  138. if (boolean_folder) {
  139.  
  140. ArrayList<String> al_path = new ArrayList<>();
  141. al_path.addAll(al_images.get(int_position).getAl_imagepath());
  142. al_path.add(absolutePathOfImage);
  143. al_images.get(int_position).setAl_imagepath(al_path);
  144.  
  145. } else {
  146. ArrayList<String> al_path = new ArrayList<>();
  147. al_path.add(absolutePathOfImage);
  148. Model_images obj_model = new Model_images();
  149. obj_model.setStr_folder(cursor.getString(column_index_folder_name));
  150. obj_model.setAl_imagepath(al_path);
  151.  
  152. al_images.add(obj_model);
  153.  
  154.  
  155. }
  156.  
  157.  
  158. }
  159.  
  160.  
  161. for (int i = 0; i < al_images.size(); i++) {
  162. Log.e("FOLDER", al_images.get(i).getStr_folder());
  163. for (int j = 0; j < al_images.get(i).getAl_imagepath().size(); j++) {
  164. Log.e("FILE", al_images.get(i).getAl_imagepath().get(j));
  165. }
  166. }
  167. obj_adapter = new Adapter_PhotosFolder(getApplicationContext(),al_images,int_position);
  168. gv_folder.setAdapter(obj_adapter);
  169. return al_images;
  170. }
  171.  
  172. @Override
  173. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  174. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  175.  
  176. switch (requestCode) {
  177. case REQUEST_PERMISSIONS: {
  178. for (int i = 0; i < grantResults.length; i++) {
  179. if (grantResults.length > 0 && grantResults[i] == PackageManager.PERMISSION_GRANTED) {
  180. fn_imagespath();
  181. } else {
  182. Toast.makeText(ImageGallery.this, "The app was not allowed to read or write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
  183. }
  184. }
  185. }
  186. }
  187. }
  188.  
  189. }
  190.  
  191. public class PhotosActivity extends AppCompatActivity {
  192. int int_position;
  193. private GridView gridView;
  194. GridViewAdapter adapter;
  195. Adapter_PhotosFolder obj_adapter;
  196. ArrayList<Model_images> al_menu = new ArrayList<>();
  197. boolean boolean_folder;
  198.  
  199. @Override
  200. protected void onCreate(Bundle savedInstanceState) {
  201. super.onCreate(savedInstanceState);
  202. setContentView(R.layout.activity_image_gallery);
  203.  
  204. final ImageButton button1 = (ImageButton) findViewById(R.id.button1);
  205. final ImageButton button2 = (ImageButton) findViewById(R.id.button2);
  206. final ImageButton button3 = (ImageButton) findViewById(R.id.button3);
  207. final ImageButton button4 = (ImageButton) findViewById(R.id.button4);
  208. final ImageButton button5 = (ImageButton) findViewById(R.id.button5);
  209. button1.setVisibility(View.GONE);
  210. button2.setVisibility(View.GONE);
  211. button3.setVisibility(View.GONE);
  212. button4.setVisibility(View.GONE);
  213. button5.setVisibility(View.GONE);
  214.  
  215. gridView = (GridView) findViewById(android.R.id.list);
  216. int_position = getIntent().getIntExtra("value", 0);
  217. adapter = new GridViewAdapter(this, al_images, int_position);
  218. gridView.setAdapter(adapter);
  219. fn_imagespath();
  220.  
  221. gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  222. @Override
  223. public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
  224. String abc = "file://" + al_images.get(int_position).getAl_imagepath().get(position);
  225.  
  226. Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
  227. i.putExtra("id", position);
  228. i.putExtra("folderPosition", int_position);
  229. i.putExtra("abc", abc);
  230. startActivity(i);
  231. }
  232. });
  233.  
  234. gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
  235. @Override
  236. public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
  237. for (int j = 0; j < parent.getChildCount(); j++)
  238. parent.getChildAt(j).setBackgroundColor(Color.TRANSPARENT);
  239.  
  240. // change the background color of the selected element
  241. view.setBackgroundColor(Color.LTGRAY);
  242. button1.setVisibility(View.VISIBLE);
  243. button2.setVisibility(View.VISIBLE);
  244. button3.setVisibility(View.VISIBLE);
  245. button4.setVisibility(View.VISIBLE);
  246. button5.setVisibility(View.VISIBLE);
  247. button3.setOnClickListener(
  248. new View.OnClickListener() {
  249. public void onClick(View view) {
  250. AlertDialog.Builder builder1 = new AlertDialog.Builder(PhotosActivity.this);
  251. builder1.setMessage("Are you sure you want to delete it ?");
  252. builder1.setCancelable(true);
  253.  
  254. builder1.setPositiveButton(
  255. "Yes",
  256. new DialogInterface.OnClickListener() {
  257. public void onClick(DialogInterface dialog, int id) {
  258. File file = new File( al_menu.get(int_position).getAl_imagepath().get(position));
  259. file.delete();
  260. MediaScannerConnection.scanFile(PhotosActivity.this,new String[] { file.toString() }, null,
  261. new MediaScannerConnection.OnScanCompletedListener() {
  262. public void onScanCompleted(String path, Uri uri) {
  263. Log.i("ExternalStorage", "Scanned " + path + ":");
  264. Log.i("ExternalStorage", "-> uri=" + uri);
  265. }
  266. });
  267. al_menu.remove(position);
  268. adapter.notifyDataSetChanged();
  269. finish();
  270. }
  271. });
  272.  
  273. builder1.setNegativeButton(
  274. "No",
  275. new DialogInterface.OnClickListener() {
  276. public void onClick(DialogInterface dialog, int id) {
  277. dialog.cancel();
  278. }
  279. });
  280.  
  281. AlertDialog alert11 = builder1.create();
  282. alert11.show();
  283. }
  284. });
  285.  
  286. return true;
  287. }
  288. });
  289. }
  290. public ArrayList<Model_images> fn_imagespath() {
  291. al_menu.clear();
  292.  
  293. int int_position = 0;
  294. Uri uri;
  295. Cursor cursor;
  296. int column_index_data, column_index_folder_name;
  297.  
  298. String absolutePathOfImage;
  299. uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  300.  
  301. String[] projection = {MediaStore.MediaColumns.DATA, MediaStore.Images.Media.BUCKET_DISPLAY_NAME};
  302.  
  303. final String orderBy = MediaStore.Images.Media.DATE_TAKEN;
  304. cursor = getApplication().getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
  305.  
  306. column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
  307. column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
  308. while (cursor.moveToNext()) {
  309. absolutePathOfImage = cursor.getString(column_index_data);
  310. Log.e("Column", absolutePathOfImage);
  311. Log.e("Folder", cursor.getString(column_index_folder_name));
  312.  
  313. for (int i = 0; i < al_menu.size(); i++) {
  314. if (al_menu.get(i).getStr_folder().equals(cursor.getString(column_index_folder_name))) {
  315. boolean_folder = true;
  316. int_position = i;
  317. break;
  318. } else {
  319. boolean_folder = false;
  320. }
  321. }
  322.  
  323.  
  324. if (boolean_folder) {
  325.  
  326. ArrayList<String> al_path = new ArrayList<>();
  327. al_path.addAll(al_menu.get(int_position).getAl_imagepath());
  328. al_path.add(absolutePathOfImage);
  329. al_menu.get(int_position).setAl_imagepath(al_path);
  330.  
  331. } else {
  332. ArrayList<String> al_path = new ArrayList<>();
  333. al_path.add(absolutePathOfImage);
  334. Model_images obj_model = new Model_images();
  335. obj_model.setStr_folder(cursor.getString(column_index_folder_name));
  336. obj_model.setAl_imagepath(al_path);
  337.  
  338. al_menu.add(obj_model);
  339.  
  340.  
  341. }
  342.  
  343.  
  344. }
  345.  
  346.  
  347. for (int i = 0; i < al_menu.size(); i++) {
  348. Log.e("FOLDER", al_menu.get(i).getStr_folder());
  349. for (int j = 0; j < al_menu.get(i).getAl_imagepath().size(); j++) {
  350. Log.e("FILE", al_menu.get(i).getAl_imagepath().get(j));
  351. }
  352. }
  353. adapter = new GridViewAdapter(this, al_menu,int_position);
  354. gridView.setAdapter(adapter);
  355. return al_menu;
  356. }
  357. }
  358.  
  359. public class Adapter_PhotosFolder extends ArrayAdapter<Model_images> {
  360.  
  361. Context context;
  362. ViewHolder viewHolder;
  363. ArrayList<Model_images> al_menu = new ArrayList<>();
  364. int int_position;
  365.  
  366.  
  367. public Adapter_PhotosFolder(Context context, ArrayList<Model_images> al_menu,int position) {
  368. super(context, R.layout.activity_adapter__photos_folder, al_menu);
  369. this.al_menu = al_menu;
  370. this.context = context;
  371. this.int_position=position;
  372. }
  373.  
  374. @Override
  375. public int getCount() {
  376.  
  377. Log.e("ADAPTER LIST SIZE", al_menu.size() + "");
  378. return al_menu.size();
  379. }
  380.  
  381. @Override
  382. public int getItemViewType(int position) {
  383. return position;
  384. }
  385.  
  386. @Override
  387. public int getViewTypeCount() {
  388. if (al_menu.size() > 0) {
  389. return al_menu.size();
  390. } else {
  391. return 1;
  392. }
  393. }
  394.  
  395. @Override
  396. public long getItemId(int position) {
  397. return position;
  398. }
  399.  
  400.  
  401. @Override
  402. public View getView(final int position, View convertView, ViewGroup parent) {
  403.  
  404. if (convertView == null) {
  405.  
  406. viewHolder = new ViewHolder();
  407. convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_adapter__photos_folder, parent, false);
  408. viewHolder.tv_foldern = (TextView) convertView.findViewById(R.id.tv_folder);
  409. viewHolder.tv_foldersize = (TextView) convertView.findViewById(R.id.tv_folder2);
  410. viewHolder.iv_image = (ImageView) convertView.findViewById(R.id.iv_image);
  411.  
  412.  
  413. convertView.setTag(viewHolder);
  414. } else {
  415. viewHolder = (ViewHolder) convertView.getTag();
  416. }
  417.  
  418. viewHolder.tv_foldern.setText(al_menu.get(position).getStr_folder());
  419. viewHolder.tv_foldersize.setText(al_menu.get(position).getAl_imagepath().size()+"");
  420.  
  421.  
  422.  
  423. Glide.with(context).load("file://" + al_menu.get(position).getAl_imagepath().get(0))
  424. .diskCacheStrategy(DiskCacheStrategy.NONE)
  425. .skipMemoryCache(true)
  426. .into(viewHolder.iv_image);
  427.  
  428.  
  429. return convertView;
  430.  
  431. }
  432.  
  433. private static class ViewHolder {
  434. TextView tv_foldern, tv_foldersize;
  435. ImageView iv_image;
  436.  
  437.  
  438. }
Add Comment
Please, Sign In to add comment