Intent photoIntent = new Intent(Intent.ACTION_PICK);
photoIntent.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
photoIntent.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(photoIntent, REQUEST_PHOTO);

갤러리 호출 




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_PHOTO:
if (data != null) {

String filePath = null;
Bitmap selectedImage = null;


if (data.getData() != null) {
filePath = Util.UpimageSave(SignModifyActivity.this, data, Util.getRealPathFromURI(this, data.getData()), Global.IMGSAVEDIR, 720 * 1280, 0, 100);
selectedImage = BitmapFactory.decodeFile(Global.IMGSAVEDIR +filePath);
}

if (selectedImage == null) {
Util.toast(SignModifyActivity.this, "갤러리를 사용해주세요.", 1);
return;
}

arrNormalFilePath = Global.IMGSAVEDIR +filePath;
ivTumbnail.setImageBitmap(selectedImage);
bImgModify = true;
strImgModity = "1";
selectedImage = null;
}
break;

}
}
}




public static String UpimageSave(Context context, Intent data, String PathName, String filePath, int limitSize, int inSampleSize, int quality){
String fileName = "";

Uri imageCaptureUri = data.getData();
fileName = String.valueOf(System.currentTimeMillis()) + ".jpg";
FileOutputStream out = null;
File file = new File(filePath);
Bitmap bit = null;
BitmapFactory.Options opt = null;
AssetFileDescriptor afd = null;
if(!file.isDirectory()){
file.mkdir();
}

try {

afd = context.getContentResolver().openAssetFileDescriptor(imageCaptureUri, "r");

opt = new BitmapFactory.Options();
BitmapFactory.decodeFile (PathName, opt);

opt.inSampleSize = calculateInSampleSize(opt, 480, 360);
opt.inJustDecodeBounds = false;
bit = BitmapFactory.decodeFile(PathName, opt);

out = new FileOutputStream(filePath + fileName);

File imageFile = new File(PathName.toString());
try {
ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
int rotate = 0;
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_270:
rotate -= 90;
case ExifInterface.ORIENTATION_ROTATE_180:
rotate -= 90;
case ExifInterface.ORIENTATION_ROTATE_90:
rotate -= 90;
}

Log.i(TAG, "Detected orientation " + orientation);
if (rotate != 0) {
Log.i(TAG, "Rotating... " + rotate);
bit = bit.copy(Bitmap.Config.ARGB_8888, true);
Matrix matrix = new Matrix();
matrix.postRotate(-rotate);
bit = Bitmap.createBitmap(bit, 0, 0, bit.getWidth(),
bit.getHeight(), matrix, true);
}
} catch (IOException io) {
Log.i(TAG, "EXIF failed: " + io.toString());
}

bit.compress(CompressFormat.JPEG, quality, out);
out.close();
if(bit != null){
Util.toast(context, "사진이 첨부되었습니다.", 1);
}

} catch (Exception e) {
e.printStackTrace();
showDialog((Activity)context, "알림","변경에 실패 하였습니다. 다시 시도해 주십시오.", null);

} finally {
try{
if(afd != null){
afd.close();
afd = null;
}
if(bit != null){
bit.recycle();
bit = null;
}
out.close();
}catch (Exception e) {
e.printStackTrace();

}
}

if(!imagReSizeQuality(Global.IMGSAVEDIR + fileName, limitSize)){
Util.showDialog((Activity) context, "알림", "사진 용량이 큽니다.(5MB이하)", null);
deleteFile(filePath, fileName);
fileName = "";
return fileName;
}

return fileName;
}



public static 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 > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;

// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}

return inSampleSize;
}


'안드로이드' 카테고리의 다른 글

해상도  (0) 2017.08.18
프래그먼트에서 OptionMenu 사용  (0) 2017.04.07
안드로이드 스튜디오 단축키  (0) 2017.04.05
RecyclerView (admob) 추가하기  (0) 2017.04.05
CoordinatorLayout  (0) 2017.04.05

+ Recent posts