使用相机拍摄多张图像并插入多个不同的imageViews

时间:2013-03-18 08:55:26

标签: android

public void onClick(View v) {
    // TODO Auto-generated method stub

    switch (v.getId()) {

    case R.id.image1:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, cameraData);

        break;
    case R.id.image2:
        ii = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(ii, cameraData);
    }
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        view1.setImageBitmap(bmp);
        view2.setImageBitmap(bmp);
    }
}

我想用相机拍摄两张图片并将这些图片显示为两张独立的ImageViews。我拍了一张照片,但它在ImageViews显示了相同的图片。请帮助我真的卡住。

3 个答案:

答案 0 :(得分:1)

你可以尝试一下:

public void onClick(View v) { // TODO Auto-generated method stub

    switch (v.getId()) {

    case R.id.image1:
        i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i, 1);

        break;
    case R.id.image2:
        ii = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(ii, 2);
  }
}

然后,

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
    case 1:
       if(resultCode == RESULT_OK){
          Bundle extras = data.getExtras();
          bmp = (Bitmap) extras.get("data");
          view1.setImageBitmap(bmp);
       }
       break;
    case 2:
       if(resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) extras.get("data");
            view2.setImageBitmap(bmp);
        }
       break;
     }
}

答案 1 :(得分:1)

试试这个:

public void onClick(View v) {
        // TODO Auto-generated method stub

        switch (v.getId()) {
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        case R.id.image1:

            startActivityForResult(i, 101);

            break;
        case R.id.image2:

            startActivityForResult(i, 102);
      }
}


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (if (requestCode == 101 && resultCode == RESULT_OK)) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) extras.get("data");
            view1.setImageBitmap(bmp);

}
        if (if (requestCode ==102 && resultCode == RESULT_OK)) {
            Bundle extras = data.getExtras();
            bmp = (Bitmap) extras.get("data");

            view2.setImageBitmap(bmp);
}

答案 2 :(得分:0)

private String uploadFile() {
        String responseString = null;

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 75, bos);
        bitmap = Bitmap.createScaledBitmap(bitmap, 300, 300, true);
        byte[] data = bos.toByteArray();

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Apis.Base);


        try {
            AndroidMultiPartEntity entity = new AndroidMultiPartEntity(
                    new AndroidMultiPartEntity.ProgressListener() {

                        @Override
                        public void transferred(long num) {
                            publishProgress((int) ((num / (float) totalSize) * 100));
                        }
                    });


            ByteArrayBody bab = new ByteArrayBody(data, "bt.jpg");
            entity.addPart("gallery", bab);
            entity.addPart("action", new StringBody("update_photos"));
            entity.addPart("user_id", new StringBody(SaveSharedPreference.getUserID(GalleryFragment.this)));


            totalSize = entity.getContentLength();
            httppost.setEntity(entity);

            // Making server call
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity r_entity = response.getEntity();

            statusCode = response.getStatusLine().getStatusCode();
            responseString = EntityUtils.toString(r_entity);
            jsonObject = new JSONObject(responseString);
            Log.e("respon", "" + responseString);

            if (statusCode == 200) {
                // Server response
                SaveSharedPreference.setUserIMAGE(GalleryFragment.this, "https://pickpamphlet.com/PropertyApp/uploads/profile_pic/" + jsonObject.getJSONObject("data").getString("profile_pic"));

            } else if (statusCode == 201) {
                // Server response

                SaveSharedPreference.setUserIMAGE(GalleryFragment.this, "https://pickpamphlet.com/PropertyApp/uploads/profile_pic/" + jsonObject.getJSONObject("data").getString("profile_pic"));


            } else {
                responseString = "Error occurred! Http Status Code: " +
                        EntityUtils.toString(r_entity) + statusCode;
            }

        } catch (ClientProtocolException e) {
            responseString = e.toString();
        } catch (Exception e) {
            responseString = e.toString();
        }

        return responseString;
    }

创建一个类似这样的函数将图像上传到服务器并调整其大小。