获取用于在Android上调用相机意图的原始按钮的标识符

时间:2011-06-21 07:21:19

标签: android camera android-intent

如果我在一个视图上有多个按钮来调用相机意图(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)和一个ImageView用于预览每个图像,我需要知道onActivityResult中哪个按钮调用它,所以我知道哪个相应的预览使用如何传递识别变量?以下是仅适用于一张图片的当前代码。

图片按钮:

final ImageButton cameraTakePhotoButton = (ImageButton) photoPromptOption.findViewById(R.id.cameraTakePhotoButton);

cameraTakePhotoButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
    }
});

onActivityResult:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (resultCode == RESULT_OK) {
        if(requestCode == CAMERA_PIC_REQUEST) {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");  
            final ImageView questionPhotoResult = (ImageView) findViewById(R.id.questionPhotoResult);
            questionPhotoResult.setImageBitmap(thumbnail);
        } 
    }
} 

2 个答案:

答案 0 :(得分:0)

使用ListView和自定义适配器结束,然后使用类Photo对象迭代ArrayList ...当我拍摄照片然后刷新ListView时,我更新Photo对象的Bitmap(缩略图)属性。这种方法非常有效。

photoListView.setAdapter(new ArrayAdapter<Photo>(this, R.layout.photo_list_item, photosList) {
     @Override
     public View getView(final int position, View convertView, final ViewGroup parent) {

              View row = null;

              final Photo thisPhoto = getItem(position);

              if (null == convertView) {
                   LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                   row = inflater.inflate(R.layout.photo_list_item, null);
               } else {
                   row = convertView;
               }

               photoPreview.setOnClickListener(new View.OnClickListener() {
                   public void onClick(View view) {

                        File photoDirectory = new File(Environment.getExternalStorageDirectory()+"/Pictures/appName");

                        if(!photoDirectory.isDirectory()) {
                             photoDirectory.mkdir();
                        }

                        File photo = new File(Environment.getExternalStorageDirectory()+"/Pictures/appName/", thisPhoto.getId()+"_photo.jpg");
                        CameraHandlerSingleton.setPictureUri(Uri.fromFile(photo));
                        CameraHandlerSingleton.setPhotoId(thisPhoto.getId());

                        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
                        startActivityForResult(intent, CAMERA_PIC_REQUEST);

                    }
                });

                return row;

            }

        });

然后我的onActivityResult:

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK) {

        if(requestCode == CAMERA_PIC_REQUEST) {  

            Uri selectedImage = CameraHandlerSingleton.getPictureUri();
            String photoId = CameraHandlerSingleton.getPhotoId();

            getContentResolver().notifyChange(selectedImage, null);
            ContentResolver cr = getContentResolver();

            Bitmap thumbnail;

            try {
                thumbnail = Bitmap.createScaledBitmap(android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage), 300, 200, true);
                p.setThumbnail(thumbnail);
                p.setTaken(true);
            } catch(FileNotFoundException e) {
                Toast.makeText(this, "Picture not found.", Toast.LENGTH_SHORT).show();
                e.printStackTrace();
            } catch(IOException e) {
                Toast.makeText(this, "Failed to load.", Toast.LENGTH_SHORT).show();
                Log.e("Camera", e.toString());
            } catch(Exception e) {
                Toast.makeText(this, e.toString(), Toast.LENGTH_SHORT).show();
                Log.e("Camera Exception", e.toString());
                e.printStackTrace();
            }

            startActivity(getIntent());
            finish();

        }
    }
}

答案 1 :(得分:0)

让人惊讶。为什么不使用requestCode?只需使用唯一代码标记每个视图,确保它不会与您投掷的任何其他意图冲突。或者,您可以使用Object.hashCode() ,但请再次注意冲突。

cameraTakePhotoButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST + v.getTag());
    }
});

然后在你的处理程序中检查它:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (resultCode == RESULT_OK) {
        if(requestCode == CAMERA_PIC_REQUEST + button1.getTag()) {
            // do stuff
        } 
        else if (requestCode == CAMERA_PIC_REQUEST + button2.getTag()) {
            // do more stuff
        }
    }
} 

如果您要按类似方式处理许多按钮(或ListView中的项目),则可以使用Map从标记中恢复调用视图。