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 卡中选择图像或使用相机拍摄的图像的意图?

更新:我找到了一个解决方案,稍后会仔细检查并在此处发布。

4

2 回答 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);

        } 
    }
}
于 2012-11-12T09:00:10.220 回答
-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);
于 2012-11-12T08:50:32.460 回答