0

当我单击图像按钮时出现问题,会打开一个对话框,询问我是要从图库中选择还是拍照。当我从图库中选择所有正确的图像时,我将其发送到服务器。但是当我选择相机时,我可以拍照但图片没有出现,然后我看不到我发送到服务器的内容(如果有的话)。这是代码:

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

}

4

1 回答 1

0

您想启动意图并告诉它在哪里写入文件,然后当意图返回时,您知道文件的保存位置:

    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);
于 2013-01-15T00:12:12.703 回答