来自相机或画廊的图片?

时间:2013-05-06 02:11:17

标签: android android-intent gallery android-camera-intent

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

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

谢谢你的帮助!

2 个答案:

答案 0 :(得分: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();

这是结果(我仍然坚持认为你的代码提供了一个更好的选择集,但同样,不是你的用例或我的中最简单的东西):

enter image description here

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

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

    }
};

<强>更新:

发现它!!有一个answer here on SO可以完全满足您的需求。它仍然是各种*的解决方法。从某种意义上说,它不依赖于不同的requestCodes。但仍然有效。

奇怪的是,当我遇到这个时,我错过了它。 : - (

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

答案 1 :(得分: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;
         }
}