@SuppressLint
(
"NewApi"
)
public
class
UploadTest
extends
Activity {
ProgressDialog prgDialog;
String encodedString1 ,encodedString2;
RequestParams params =
new
RequestParams();
String imgPath1, imgPath2, fileName1, fileName2;
Bitmap bitmap1 , bitmap2;
private
static
int
RESULT_LOAD_IMG1 =
1
;
private
static
int
RESULT_LOAD_IMG2 =
1
;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.upload_test);
prgDialog =
new
ProgressDialog(
this
);
prgDialog.setCancelable(
false
);
}
public
void
loadImagefromGallery1(View view) {
Intent galleryIntent =
new
Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG1);
}
public
void
loadImagefromGallery2(View view) {
Intent galleryIntent =
new
Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG2);
}
@Override
protected
void
onActivityResult(
int
requestCode,
int
resultCode, Intent data) {
super
.onActivityResult(requestCode, resultCode, data);
try
{
if
(requestCode == RESULT_LOAD_IMG1 && resultCode == RESULT_OK &&
null
!= data) {
Uri selectedImage1 = data.getData();
String[] filePathColumn1 = { MediaStore.Images.Media.DATA };
Cursor cursor1 = getContentResolver().query(selectedImage1, filePathColumn1,
null
,
null
,
null
);
cursor1.moveToFirst();
int
columnIndex1 = cursor1.getColumnIndex(filePathColumn1[
0
]);
imgPath1 = cursor1.getString(columnIndex1);
cursor1.close();
ImageView imgView1 = (ImageView) findViewById(R.id.imgView1);
imgView1.setImageBitmap(BitmapFactory.decodeFile(imgPath1));
String fileNameSegments1[] = imgPath1.split(
"/"
);
fileName1 = fileNameSegments1[fileNameSegments1.length -
1
];
params.put(
"filename1"
, fileName1);
}
else
{
Toast.makeText(
this
,
"이미지를 찾을수 없습니다."
,
Toast.LENGTH_LONG).show();
}
if
(requestCode == RESULT_LOAD_IMG2 && resultCode == RESULT_OK &&
null
!= data) {
Uri selectedImage2 = data.getData();
String[] filePathColumn2 = { MediaStore.Images.Media.DATA };
Cursor cursor2 = getContentResolver().query(selectedImage2, filePathColumn2,
null
,
null
,
null
);
cursor2.moveToFirst();
int
columnIndex2 = cursor2.getColumnIndex(filePathColumn2[
0
]);
imgPath2 = cursor2.getString(columnIndex2);
cursor2.close();
ImageView imgView2 = (ImageView) findViewById(R.id.imgView2);
imgView2.setImageBitmap(BitmapFactory.decodeFile(imgPath2));
String fileNameSegments2[] = imgPath2.split(
"/"
);
fileName2 = fileNameSegments2[fileNameSegments2.length -
1
];
params.put(
"filename2"
, fileName2);
}
else
{
Toast.makeText(
this
,
"이미지를 찾을수 없습니다."
,
Toast.LENGTH_LONG).show();
}
}
catch
(Exception e) {
Toast.makeText(
this
,
"문제 발생"
, Toast.LENGTH_LONG)
.show();
}
}
public
void
uploadImage(View v) {
if
(imgPath1 !=
null
&& !imgPath1.isEmpty()) {
prgDialog.setMessage(
"이진데이터로 이미지 변환"
);
prgDialog.show();
encodeImagetoString();
}
else
{
Toast.makeText(
getApplicationContext(),
"갤러리에서 이미지를 선택하세요"
,
Toast.LENGTH_LONG).show();
}
if
(imgPath2 !=
null
&& !imgPath2.isEmpty()) {
prgDialog.setMessage(
"이진데이터로 이미지 변환"
);
prgDialog.show();
encodeImagetoString();
}
else
{
Toast.makeText(
getApplicationContext(),
"갤러리에서 이미지를 선택하세요"
,
Toast.LENGTH_LONG).show();
}
}
public
void
encodeImagetoString() {
new
AsyncTask() {
protected
void
onPreExecute() {
}
@Override
protected
String doInBackground(Void... params) {
BitmapFactory.Options options1 =
null
;
BitmapFactory.Options options2 =
null
;
options1 =
new
BitmapFactory.Options();
options2 =
new
BitmapFactory.Options();
options1.inSampleSize =
3
;
options2.inSampleSize =
3
;
bitmap1 = BitmapFactory.decodeFile(imgPath1, options1);
bitmap2 = BitmapFactory.decodeFile(imgPath2, options2);
ByteArrayOutputStream stream1 =
new
ByteArrayOutputStream();
ByteArrayOutputStream stream2 =
new
ByteArrayOutputStream();
bitmap1.compress(Bitmap.CompressFormat.JPEG,
50
, stream1);
bitmap2.compress(Bitmap.CompressFormat.JPEG,
50
, stream2);
byte
[] byte_arr1 = stream1.toByteArray();
byte
[] byte_arr2 = stream2.toByteArray();
encodedString1 = Base64.encodeToString(byte_arr1,
0
);
encodedString2 = Base64.encodeToString(byte_arr2,
0
);
return
""
;
}
@Override
protected
void
onPostExecute(String msg) {
prgDialog.setMessage(
"Calling Upload"
);
params.put(
"image1"
, encodedString1);
params.put(
"image2"
, encodedString2);
triggerImageUpload();
}
}.execute(
null
,
null
,
null
);
}
@Override
protected
void
onDestroy() {
super
.onDestroy();
if
(prgDialog !=
null
) {
prgDialog.dismiss();
}
}
}