-
Notifications
You must be signed in to change notification settings - Fork 0
/
Day_2.java
118 lines (99 loc) · 3.36 KB
/
Day_2.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package net.androidbootcamp.hotfoot;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.provider.MediaStore;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.ArrayList;
import java.util.List;
public class Day_2 extends AppCompatActivity
{
Uri imageUri;
Uri[] images = new Uri[100];
ArrayList<Uri> uploadedImages = new ArrayList<Uri>();
int index = 0;
Button upload;
ImageView imageView;
private static final int PICK_IMAGE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_day_2);
final GridView gridView = (GridView) findViewById(R.id.day2GridView);
final ImageAdapter adapter = new ImageAdapter(this);
gridView.setAdapter(adapter);
adapter.setImageList(uploadedImages);
upload = (Button)findViewById(R.id.btnUpload);
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//open the phone's gallery
OpenGallery();
adapter.updateImageList(uploadedImages);
}
});
}
//a method for accessing the phone's internal media
public void OpenGallery(){
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == PICK_IMAGE){
imageUri = data.getData();
//add the image to the adapter to be added to the list
uploadedImages.add(index,imageUri);
}
}
//The adapter for the GridView
public class ImageAdapter extends BaseAdapter
{
private Context context;
ArrayList<Uri> imagelist = new ArrayList<Uri>();
public ImageAdapter(Context c) {
context = c;
}
@Override
public int getCount() {
return index;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
imageView = new ImageView(context);
imageView.setImageURI(uploadedImages.get(position));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new GridView.LayoutParams(330, 300));
return imageView;
}
public void setImageList(ArrayList<Uri> imagelist)
{
this.imagelist = imagelist;
}
public void updateImageList(ArrayList<Uri> newImagelist)
{
if(this.imagelist != null)
{
imagelist.addAll(newImagelist);
}
notifyDataSetChanged();
}
}
}