package
com.example.seesaw;
import
java.io.File;
import
java.io.IOException;
import
java.util.Locale;
import
android.content.Context;
import
android.graphics.Bitmap;
import
android.graphics.BitmapFactory;
import
android.graphics.Matrix;
import
android.media.ExifInterface;
import
android.os.Environment;
import
android.util.Log;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseAdapter;
import
android.widget.GridView;
import
android.widget.ImageView;
import
android.view.WindowManager;
public
class
ImageAdapter
extends
BaseAdapter{
private
Context mContext;
String [] filename, filepath;
int
k;
int
orientation, rotation;
Bitmap bitmap, rotateBitmap, resizebitmap, cutbitmap;
ImageView imageView;
public
ImageAdapter(Context c) {
mContext = c;
}
@Override
public
int
getCount() {
imagesetting();
return
k;
}
@Override
public
Object getItem(
int
position) {
return
null
;
}
@Override
public
long
getItemId(
int
position) {
return
0
;
}
int
height, width;
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
height = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getHeight();
width = ((WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
if
(convertView ==
null
){
imageView =
new
ImageView(mContext);
imageView.setLayoutParams(
new
GridView.LayoutParams(width*
212
/
720
,height*
212
/
1280
));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(
8
,
8
,
8
,
8
);
}
else
{
imageView = (ImageView)convertView;
}
imageView.setBackgroundResource(R.drawable.photo_frame);
imageView.setImageBitmap(bitmapcreate(position));
return
imageView;
}
public
void
imagesetting(){
filename =
null
;
filepath =
null
;
File f =
new
File(Environment.getExternalStorageDirectory()+
"/DCIM/Camera1/"
);
File[] files = f.listFiles();
k =
0
;
filename =
new
String[files.length];
filepath =
new
String[files.length];
for
(
int
i=
0
;i<files.length;i++){
File file = files[i];
String curFile = file.getPath();
String ext = curFile.substring(curFile.lastIndexOf(
"."
)+
1
,curFile.length()).toLowerCase(Locale.KOREA);
if
(ext.equals(
"jpg"
) || ext.equals(
"jpeg"
) || ext.equals(
"gif"
) || ext.equals(
"png"
)){
filename[k] = file.getName();
filepath[k] = file.getPath();
k = k +
1
;
}
}
}
public
Bitmap bitmapcreate(
int
i){
bitmap =
null
;
rotateBitmap =
null
;
resizebitmap =
null
;
cutbitmap =
null
;
BitmapFactory.Options bmOptions =
new
BitmapFactory.Options();
bmOptions.inJustDecodeBounds =
true
;
BitmapFactory.decodeFile(filepath[i], bmOptions);
bmOptions.inJustDecodeBounds =
false
;
if
(bmOptions.outWidth >
1600
) {
bmOptions.inSampleSize =
8
;
}
else
{
bmOptions.inSampleSize =
2
;
}
bmOptions.inPurgeable =
true
;
bitmap = BitmapFactory.decodeFile(filepath[i], bmOptions);
try
{
ExifInterface mExif =
new
ExifInterface(filepath[i]);
orientation = Integer.parseInt(mExif.getAttribute(ExifInterface.TAG_ORIENTATION));
}
catch
(IOException e){
Log.i(
"IOException e :"
,
""
+e);
e.printStackTrace();
}
catch
(Exception e){
Log.i(
"Exception e :"
,
""
+e);
}
if
(orientation ==
6
){
rotation =
90
;
}
else
if
(orientation ==
8
){
rotation =
270
;
}
else
if
(orientation ==
3
){
rotation =
180
;
}
else
if
((orientation ==
1
) || (orientation ==
0
)){
rotation =
0
;
}
Matrix matrix =
new
Matrix();
matrix.postRotate(rotation);
rotateBitmap = Bitmap.createBitmap(bitmap,
0
,
0
,bitmap.getWidth(),bitmap.getHeight(),matrix,
true
);
if
(bitmap.getWidth() > bitmap.getHeight()){
if
(rotation ==
0
|| rotation ==
180
){
resizebitmap = Bitmap.createScaledBitmap(rotateBitmap, (
int
)((
212
*rotateBitmap.getWidth()/rotateBitmap.getHeight())*width/
720
), (
int
)(
212
*height/
1280
),
true
);
cutbitmap = Bitmap.createBitmap(resizebitmap, ((resizebitmap.getWidth()-resizebitmap.getHeight())/
2
),
0
, (
int
)(
212
*width/
720
), (
int
)(
212
*height/
1280
));
}
else
if
(rotation ==
90
|| rotation ==
270
){
resizebitmap = Bitmap.createScaledBitmap(rotateBitmap, (
int
)(
212
*width/
720
), (
int
)((
212
*rotateBitmap.getHeight()/rotateBitmap.getWidth())*height/
1280
),
true
);
cutbitmap = Bitmap.createBitmap(resizebitmap,
0
, ((resizebitmap.getHeight()-resizebitmap.getWidth())/
2
), (
int
)(
212
*width/
720
), (
int
)(
212
*height/
1280
));
}
}
else
if
(bitmap.getWidth() < bitmap.getHeight()){
if
(rotation ==
0
|| rotation ==
180
){
resizebitmap = Bitmap.createScaledBitmap(rotateBitmap, (
int
)(
212
*width/
720
), (
int
)((
212
*rotateBitmap.getHeight()/rotateBitmap.getWidth())*height/
1280
),
true
);
cutbitmap = Bitmap.createBitmap(resizebitmap,
0
,((resizebitmap.getHeight()-resizebitmap.getWidth())/
2
), (
int
)(
212
*width/
720
), (
int
)(
212
*height/
1280
));
}
else
if
(rotation ==
90
|| rotation ==
270
){
}
}
else
if
(bitmap.getWidth() == bitmap.getHeight()){
if
(rotation ==
0
|| rotation ==
180
){
resizebitmap = Bitmap.createScaledBitmap(rotateBitmap, (
int
)(
212
*width/
720
), (
int
)(
212
*height/
1280
),
true
);
cutbitmap = Bitmap.createBitmap(resizebitmap,
0
,
0
, (
int
)(
212
*width/
720
), (
int
)(
212
*height/
1280
));
}
else
if
(rotation ==
90
|| rotation ==
270
){
}
}
bitmap = cutbitmap;
return
bitmap;
}
}