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

发布于 2024-11-16 21:40:11 字数 1078 浏览 1 评论 0原文

如果我在视图上有多个按钮来调用相机意图(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);
        } 
    }
} 

If I have multiple buttons on a view to call camera intent (android.provider.MediaStore.ACTION_IMAGE_CAPTURE) and a ImageView for the preview of each image and I need to know which button called it in onActivityResult so I know which corresponding preview to use how do I pass an identifying variable? Below is current code that only works with one image.

Picture button:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寻梦旅人 2024-11-23 21:40:11

最终使用 ListView 和自定义适配器,然后让它使用 Photo 类的对象迭代 ArrayList...当我拍照时,我会更新 Photo 对象的位图(缩略图)属性,然后刷新 ListView。这个方法效果很好。

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();

        }
    }
}

Ended up using a ListView and custom adapter then having it iterate over an ArrayList with objects of class Photo... I update the Bitmap (thumbnail) property for the Photo objects when I take the picture then refresh the ListView. This method works very well.

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;

            }

        });

Then my 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();

        }
    }
}
小瓶盖 2024-11-23 21:40:11

哎呀。为什么不使用 requestCode 呢?只需用唯一的代码标记每个视图,确保它不会与您抛出的任何其他意图发生冲突。或者,您可以使用 对象。 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 从标记恢复调用视图。

Yikes. Why not use requestCode? Just tag each view with a unique code, making sure it doesn't clash with any other intents you're throwing around. Alternatively, you can use Object.hashCode() , but again, watch out for clashes.

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

Then check it in your handler:

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

If you have many buttons (or items in a ListView) that you're going to handle similarly, you can use a Map to recover the calling view from the tag.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文