从SD卡和相机获取照片

4

当我触发这个意图时,会显示一个标准的图片选择对话框(从SD卡中选择):

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);

这将列出所有可以返回图像(例如相册)的应用程序/活动。

在这个标准列表中,我还想包括一个选项,可以启动相机并返回拍摄的图像。问题是,我无法找到一种非自定义方式来实现这一点(使用自定义布局、应用程序图像+标题等构建我的对话框)。

相机活动可以这样启动:

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
URI pictureUri = Uri.fromFile(new File("dummyPath"));
camera.putExtra(MediaStore.EXTRA_OUTPUT, pictureUri);

有没有一种意图可以让我从SD卡或相机拍摄的照片中选择一张图片?

更新:我已经找到了解决方案,稍后会再次确认并在这里发布。


请问您能分享解决方案吗? - Swapnil Sonar
2个回答

4

你可以使用警告对话框来显示选项。以下是源代码:

AlertDialog.Builder getImageFrom = new AlertDialog.Builder(MainActivity.this);
            getImageFrom.setTitle("Select Image");
            final CharSequence[] opsChars = {"Take Picture", "Open Gallery"};
            getImageFrom.setItems(opsChars, new android.content.DialogInterface.OnClickListener(){
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(which == 0){
                         File file = new File( _path );
                         outputFileUri = Uri.fromFile( file );

                        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                        cameraIntent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);   

                        startActivityForResult(cameraIntent, 7);
                    }else
                        if(which == 1){
                            Intent intent = new Intent();
                            intent.setType("image/*");
                            intent.setAction(Intent.ACTION_GET_CONTENT);
                            startActivityForResult(Intent.createChooser(intent,
                                "Open Gallery"), 6);
                        }
                    dialog.dismiss();
                }
            });


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 6) {
            Uri selectedImageUri = data.getData();
         pickerImageView.setPadding(0, 0, 0, 0);
         pickerImageView.setScaleType(ScaleType.FIT_XY);
         System.gc();
           String filepath = getPath(selectedImageUri);
           File imagefile = new File(filepath);
           try {
            FileInputStream fis = new FileInputStream(imagefile);
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inPurgeable=true;
            options.inSampleSize =4;
            bi= BitmapFactory.decodeStream(fis,null,options);
            fis.close();
            Bitmap bitmapToRecycle = ((BitmapDrawable)pickerImageView.getDrawable()).getBitmap();  
            bitmapToRecycle.recycle();
            pickerImageView.setImageBitmap(bi); 
            pickerImageView.setPadding(0, 0, 0, 0);
            pickerImageView.setScaleType(ScaleType.FIT_XY);


        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
           pickImageTextView.setText("");
        }
        else if(requestCode == 7){
            Log.i("return", "#####");
            BitmapFactory.Options options=new BitmapFactory.Options();
            options.inPurgeable=true;
            options.inSampleSize =4;
            //Bitmap photo = BitmapFactory.decodeFile( _path, options );
             Bitmap photo = (Bitmap) data.getExtras().get("data"); 
             pickerImageView.setImageBitmap(photo); 
             pickerImageView.setPadding(0, 0, 0, 0);
             pickerImageView.setScaleType(ScaleType.FIT_XY);

        } 
    }
}

这正是我想避免的情况:创建自定义对话框很麻烦,而且需要时间。如果有标准(可能更简单)的解决方案,我想使用它。 - Buffalo
我已经尝试了你的代码,但似乎它并没有起作用。在拍照后,相机活动就一直卡在那里。我该如何返回到我的活动?是否需要进行任何特殊操作? - Buffalo
onActivityResult() 方法代码返回到活动。在这里,将选定的图像设置为 ImageView 的 m 变量。 - Sharmilee
我理解,但是:点击该选项会显示一个带有相机选项的对话框(正确)。点击相机选项会打开相机活动(正确)。然而:在相机活动中点击“拍照”后,什么也没有发生!正常行为不应该是返回到我的活动,并在onActivityResult方法中吗? - Buffalo
http://mobile.tutsplus.com/tutorials/android/android-sdk-quick-tip-launching-the-camera/ - Sharmilee
显然,在2.2设备上,您需要点击一个按钮来“使用”最近拍摄的照片。我的另一个问题是由于图像确实保存到提供的URI中,但在某些设备上,返回的意图将为空。感谢您的帮助。 - Buffalo

-1

您可以通过编程的方式将图片添加到相册中,方法如下:

Intent media = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);

//mCurrentPhotoPath是图像的路径。

File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
media.setData(contentUri);
this.sendBroadcast(media);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接