Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 从SD卡和相机获取图片大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在启动这个意图时显示的标准图像选择(来自SD卡)对话框:

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

这列出了可以返回图像的所有应用程序/活动(例如图库).

在同一标准列表中,我还想包含一个选项,它将启动相机并返回使用它拍摄的图像.问题是我无法弄清楚如何以非自定义方式执行此操作(使用自定义布局,应用程序图像标题等构建我自己的对话框).

Camera活动可以像这样启动:

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

是否有意图允许我从SD卡或使用相机拍摄的图像中选择图像?

更新:我找到了解决方案,会仔细检查并在此后发布.

解决方法

您可以使用警报对话框来显示选项.
代码如下:

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);
         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);
            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);
             pickerImageView.setScaleType(ScaleType.FIT_XY);

        } 
    }
}

大佬总结

以上是大佬教程为你收集整理的android – 从SD卡和相机获取图片全部内容,希望文章能够帮你解决android – 从SD卡和相机获取图片所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。