画像ボタンをクリックすると、ギャラリーから選択するか、写真を撮るかを尋ねるダイアログが表示されます。ギャラリーから選択すると、すべての適切な画像が表示され、サーバーに送信されます。しかし、カメラを選択すると写真を撮ることができますが、写真が表示されず、サーバーに送信したものが表示されません。コードは次のとおりです。
public class MainActivity extends Activity {
private final String PHP_URL = "http://wapps.no-ip.org/testservice/service.php";
private EditText fldLocation, fldDescription;
ImageView targetImage, btnLocation;
Button btnSend;
JSONParser jParser;
HttpResponse response;
Intent intent;
Bitmap theImage;
String selectedImagePath;
Uri imageUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
// ===============================================
// tag elements from layout
// ===============================================
fldLocation = (EditText) findViewById(R.id.fldLocation);
fldDescription = (EditText) findViewById(R.id.fldDescription);
btnSend = (Button) findViewById(R.id.btnSend);
targetImage = (ImageView) findViewById(R.id.targetimage);
btnLocation = (ImageView) findViewById(R.id.btnLocation);
registerForContextMenu(btnSend);
targetImage.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openAddPhoto();
}
});
btnSend.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
MakePost();
}
});
}
public void MakePost() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PHP_URL);
//targetImage.buildDrawingCache();
//theImage = targetImage.getDrawingCache();
//theImage = BitmapFactory.decodeFile(selectedImagePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
theImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
String str = Base64.encodeBytes(byteArray);
try {
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("addimg", "true"));
nameValuePairs.add(new BasicNameValuePair("img", str));
//nameValuePairs.add(new BasicNameValuePair("img",f.getAbsolutePath()));
nameValuePairs.add(new BasicNameValuePair("location", fldLocation
.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("desc", fldDescription
.getText().toString()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
httpclient.execute(httppost);// HttpResponse response =
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
public void openAddPhoto() {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Choose path");
String[] items = { "Camera", "Gallery" };
dialog.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
if (id == 1) {
// call gallery
Intent pickPhoto = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, 1);
} else {
// Call camera Intent
Intent takePicture = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);
}
}
});
dialog.setNeutralButton("cancel",
new android.content.DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dialog.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 0:
if (resultCode == RESULT_OK) {
imageUri = data.getData();
fldDescription.setText(imageUri.toString());
try {
theImage = Media.getBitmap(this.getContentResolver(), imageUri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
break;
case 1:
if (resultCode == RESULT_OK) {
imageUri = data.getData();
fldDescription.setText(imageUri.toString());
try {
theImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
targetImage.setImageURI(imageUri);
}
break;
}
}
}