17

我有一个意图选择器,允许我从画廊或相机中选择图像,如下所示:

    Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null);
    galleryIntent.setType("image/*");
    galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 


    Intent chooser = new Intent(Intent.ACTION_CHOOSER);
    chooser.putExtra(Intent.EXTRA_INTENT, galleryIntent);      
    chooser.putExtra(Intent.EXTRA_TITLE, "title");

    Intent[] intentArray =  {cameraIntent}; 
    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
    startActivityForResult(chooser,REQUEST_CODE);

我希望我的onActivityResult方法是这样的:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(Condition == picture_coming_from_gallery)
    {
     //my code here
    }
    else if(condition == picture_coming_from_camera)
    {
     //another code here
    }
}

什么条件可以让我知道我的图像来自哪个来源?

更新:

现在它正在工作,这是解决方案:

    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
    {
        if(data.getData()!=null)
        {
            try 
            {
            if (bitmap != null) 
                {
                    bitmap.recycle();
                }

            InputStream stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            stream.close();
            imageView.setImageBitmap(bitmap);
            }

        catch (FileNotFoundException e) 
            {
                e.printStackTrace();
            }

        catch (IOException e) 
            {
                e.printStackTrace();
            }
        } 

        else 
        {
            bitmap=(Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(bitmap);
        }

        super.onActivityResult(requestCode, resultCode, data);
    }
}

谢谢你的帮助!

4

2 回答 2

12

尽管当前的代码是一种呈现可供选择的选项的简洁方式,但我发现它非常难以管理。至少在我的用例中是这样。我需要存储从相机拍摄的图像以在 Aviary SDK 中进一步处理(如果用户选择)。

为此,我想提出一个解决方法。

这本身并不能解决您的问题。但是考虑到您需要知道图像的来源(相机/画廊),提供了另一种选择

AlertDialog.Builder builder = new AlertDialog.Builder(StatusUpdate.this);
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] {"Gallery", "Camera"}, 
        new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
        case 0:

            // GET IMAGE FROM THE GALLERY
            Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");

            Intent chooser = Intent.createChooser(intent, "Choose a Picture");
            startActivityForResult(chooser, ACTION_REQUEST_GALLERY);

            break;

        case 1:
            Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");

            File cameraFolder;

            if (android.os.Environment.getExternalStorageState().equals
                    (android.os.Environment.MEDIA_MOUNTED))
                cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),
                        "some_directory_to_save_images/");
            else
                cameraFolder= StatusUpdate.this.getCacheDir();
            if(!cameraFolder.exists())
                cameraFolder.mkdirs();

            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
            String timeStamp = dateFormat.format(new Date());
            String imageFileName = "picture_" + timeStamp + ".jpg";

            File photo = new File(Environment.getExternalStorageDirectory(), 
                    "some_directory_to_save_images/" + imageFileName);
            getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
            initialURI = Uri.fromFile(photo);

            startActivityForResult(getCameraImage, ACTION_REQUEST_CAMERA);

            break;

        default:
            break;
        }
    }
});

builder.show();

这就是结果(我仍然认为您的代码提供了更好的选择集,但同样,这在您的用例或我的用例中都不是最简单的):

在此处输入图像描述

现在您可以根据选择的来源处理结果:

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

        switch (requestCode) {
        case ACTION_REQUEST_GALLERY:

            break;

        case ACTION_REQUEST_CAMERA:

            break;          
        }

    }
};

更新:

找到了!!SO上有一个答案可以准确地解决您的需求。它仍然是一种*解决方法*。从某种意义上说,它并不依赖于不同的requestCodes. 但仍然有效。

奇怪的是,当我被困住时,我错过了它。:-(

注意: 我没有在这里发布代码,而是链接到它。所有道具归原作者所有。:-)

于 2013-05-06T02:52:47.657 回答
3

您可以使用 REQUEST_CODE 进行区分

    private static final int PICK_FROM_CAMERA = 1;
        private static final int PICK_FROM_GALLARY = 2;

        /* For Image capture from camera */
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(intent, PICK_FROM_CAMERA);   

    /* For Image capture from Gallary*/             
        startActivityForResult(new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI),
                                        PICK_FROM_GALLARY);
                        }



public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        switch (requestCode) {
               case PICK_FROM_CAMERA:
            if (resultCode == Activity.RESULT_OK) {
             Bitmap bitmapImage = (Bitmap) intent.getExtras().get("data");
              }
              break;

                case PICK_FROM_GALLARY:
               if (resultCode == Activity.RESULT_OK) {
              Uri mImageCaptureUri = intent.getData();
               }
             break;
         }
}
于 2013-05-06T04:59:49.867 回答