android– 从SD卡和相机获取图片

android– 从SD卡和相机获取图片,第1张

概述我正在启动这个意图时显示的标准图像选择(来自SD卡)对话框:Intentintent=newIntent();intent.setType("image/*");intent.setAction(Intent.ACTION_GET_CONTENT);startActivityForResult(Intent.createChooser(intent,"SelectPicture"),PICK_IMAGE);这列出了可以返回

我正在启动这个意图时显示的标准图像选择(来自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, 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);        }     }}
总结

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

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1118922.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存