0

I have a problem when I click the image button opens a dialogue asking me if I want to choose from the gallery or take a photo. When I select from the gallery all right image appears then I send it to the server. But when I select camera I can take pictures but the picture does not appear then I do not see what I send to the server if any. Here's the code:

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;
    }
}

}

1 Answer 1

0

You want to start the intent and tell it where to write the file, then when the intent returns you know where the file was saved:

    File out = new File(path, somefilename);
    android.net.Uri uri = android.net.Uri.fromFile(out);

    Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                
    takePicture.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);           
    startActivityForResult(takePicture, 0);
1
  • 1
    i try that still not working when i take picture on landscape mode everything perfect on portrait nothing happend i try to pass this with manifest and make the app portrait only but still only on landscape the picture showing.
    – Noni Geron
    Jan 15, 2013 at 15:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.