You are on page 1of 12

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

1 of 12

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

Rp82.400

Rp356.300

Rp415.200

Rp403.800

Rp168.200

Rp35.200

11/8/2016 7:56 PM

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

2 of 12

01
02
03
04
05
06
07
08
09
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

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

package ganti.dengan.nama.packagemu;
import
import
import
import
import
import

android.content.Context;
android.view.View;
android.view.ViewGroup;
android.widget.BaseAdapter;
android.widget.GridView;
android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {


private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
}
public static Integer[] mThumbIds =
{
//Gambar-gambar yang ada disimpan dalam array
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7,
R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5,
R.drawable.sample_6, R.drawable.sample_7
};
@Override
public int getCount() {
// Jumlah total gambar
return mThumbIds.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Mengambil satu gambar dari gallery
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}

11/8/2016 7:56 PM

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

3 of 12

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

68

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_followed_top"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:orientation="vertical"
>
<GridView
android:id="@+id/gridview_followed"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
>
</GridView>
</LinearLayout>

01
02
03
04
05
06
07
08
09
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

package ganti.dengan.nama.packagemu;

01
02
03
04
05
06
07
08
09

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

import
import
import
import
import
import
import
import
import

android.os.Bundle;
android.app.Activity;
android.content.Intent;
android.view.View;
android.view.View.OnClickListener;
android.widget.AdapterView;
android.widget.AdapterView.OnItemClickListener;
android.widget.GridView;
android.widget.Toast;

public class MainActivity extends Activity implements OnItemClickListener, OnClickListener{


@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.gridview_followed);
gridView.setAdapter(new ImageAdapter (this));
gridView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "Klik di"+position, Toast.LENGTH_SHORT).show();
Intent i = new Intent(this, SingleImage.class);
Bundle b = new Bundle();
b.putInt("posisi", position);
i.putExtras(b);
startActivity(i);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
}

<ImageView
android:id="@+id/singleimage"
android:layout_width="wrap_content"

11/8/2016 7:56 PM

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

4 of 12

10
11
12
13
14
15
16

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

android:layout_height="0dip"
android:contentDescription="gambar tunggal"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_weight="1"
/>
</LinearLayout>

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20

package ganti.dengan.nama.packagemu

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="id.sg.apollo.sg"
android:versionCode="1"
android:versionName="1.0" >

import android.app.Activity;
import android.os.Bundle;
import android.widget.ImageView;
public class SingleImage extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.singleimage);
int imId = this.getIntent().getExtras().getInt("posisi");
ImageView iv = (ImageView) findViewById(R.id.singleimage);
int image = ImageAdapter.mThumbIds[imId];
iv.setImageResource(image);
}
}

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar"
>
<activity
android:name=".MainActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SingleImage"
android:label="@string/app_name"
>
</activity>
</application>
</manifest>

11/8/2016 7:56 PM

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

5 of 12

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

11/8/2016 7:56 PM

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

6 of 12

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

Tutorial Mengambil Gambar


Lewat Kamera...

Belajar Membuat Material


Design Navigation View...

Belajar Membuat
TabLayout di Android...

Belajar Membuat...

Membuat Aplikasi Location


Based Services...

Android : Deteksi Lokasi


Pengguna Menggunakan...

Android Button Style


Guidelines : Membuat...

Belajar Membuat Material


Design CardView di And...

11/8/2016 7:56 PM

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

7 of 12

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

1
2
3
4
5
6
7
8

@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tiga);
GridView gridView = (GridView) findViewById(R.id.gridview_followed);
gridView.setAdapter(new ImageAdapter (this));
gridView.setOnItemClickListener(this);
}

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042

public class viewCamera extends Activity implements OnItemClickListener, OnClickListener {


public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList itemList = new ArrayList();
public ImageAdapter(Context c) {
mContext = c;
}
void add(String path){
itemList.add(path);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return itemList.get(arg0);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
Bitmap bitmap;
if(itemList.get(position).contains(".jpeg"))
{
bitmap = BitmapFactory.decodeFile(itemList.get(position)); //Creation of Thumbnail of image

11/8/2016 7:56 PM

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

8 of 12

043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

}
imageView.setLayoutParams(new GridView.LayoutParams(300, 300));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 300, 300);
imageView.setImageBitmap(bm);
return imageView;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height &gt; reqHeight || width &gt; reqWidth) {
if (width &gt; height) {
inSampleSize = Math.round((float)height / (float)reqHeight);
} else {
inSampleSize = Math.round((float)width / (float)reqWidth);
}
}
return inSampleSize;
}
}
ImageAdapter myImageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_camera);
GridView gridview = (GridView) findViewById(R.id.gridview);
myImageAdapter = new ImageAdapter(this);
gridview.setAdapter(myImageAdapter);
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/Pictures/ePublisher/";
Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG).show();
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith("IMG");
}
});
for (File file : files){
myImageAdapter.add(file.getAbsolutePath());
}
gridview.setOnItemClickListener(this);
}

11/8/2016 7:56 PM

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

9 of 12

134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

@Override
public void onItemClick(AdapterView parent, View v, int position, long id) {
Toast.makeText(getBaseContext(), "Photo "+(position+1)+
" selected", Toast.LENGTH_SHORT).show();
Intent i = new Intent(this, viewImage.class);
Bundle b = new Bundle();
b.putInt("posisi", position);
i.putExtras(b);
startActivity(i);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
}

public class viewImage extends Activity {


ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_image);
image=(ImageView)findViewById(R.id.full_image_view);
Intent i = getIntent();
int resId = i.getExtras().getInt("posisi");
image.setImageResource(resId);
}
}

11/8/2016 7:56 PM

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

10 of 12

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em>
<i> <q cite=""> <s> <strike> <strong>

Iklan oleh Google

Jadilah orang pertama yang menyukai


ini.

11/8/2016 7:56 PM

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

11 of 12

Follow @twoh

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

691 followers

11/8/2016 7:56 PM

Tutorial Android Membuat Image Gallery (Galeri Gambar) Menggunak...

12 of 12

http://www.twoh.co/2013/04/tutorial-android-membuat-image-gallery-gal...

11/8/2016 7:56 PM

You might also like