안드로이드에서 특정 이미지를 선택하고 파일을 업로드하는 코드입니다.
특별한 에러는 없는데 서버쪽에서 리턴값을 받아보니 이미지가 자꾸 null로 찍히네요..
도저히 무슨 문제인지 몰라서 질문드립니다. 고수님들 부탁드립니다 ㅠㅠ
tv = (TextView)findViewById(R.id.textView1);
tv.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_PICK);
intent.setType(android.provider.MediaStore.Images.Media.CONTENT_TYPE);
intent.setData(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, TAKE_PICTURE);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == TAKE_PICTURE){
if( data != null ){
//final Bundle extras = data.getExtras();
//Bitmap photo = extras.getParcelable("data");
Uri selPhotoUri = data.getData();
try{
Bitmap selPhoto = Images.Media.getBitmap(getContentResolver(),selPhotoUri);
String urlString = "http://112.108.40.202/ImageInsert.php";
Cursor c = getContentResolver().query(Uri.parse(selPhotoUri.toString()), null, null, null, null);
c.moveToFirst();
String abPath = c.getString(c.getColumnIndex(MediaStore.MediaColumns.DATA));
HttpFileUpload(urlString, "", abPath);
if( selPhoto != null ){ // 가지고온 사진 데이터를 이미지 뷰에 보여 준다.
iv.setImageBitmap(selPhoto);
tv.setText(abPath.toString());
//signup_camera.setImageBitmap(photo);
//Uri currImageURI = data.getData();
//textView.setText("GALLERY : " + getRealPathFromURI(currImageURI));
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}finally{
}
}
}
super.onActivityResult(requestCode, resultCode, data);
}
public String HttpFileUpload(String urlString, String params, String imgpath) {
String result = "";
try {
File file = new File(imgpath);
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpPost httppost = new HttpPost(urlString);
MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbFile = new FileBody(file, "image/jpeg");
mpEntity.addPart("MEM_image", cbFile);
httppost.setEntity(mpEntity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
result = EntityUtils.toString(resEntity);
Log.e("RESPONSE", result);
if (resEntity != null) {
}
httpclient.getConnectionManager().shutdown();
}catch(Exception e){
e.printStackTrace();
}
return result;
}