Android–ImageView不会显示用相机拍摄的照片

Android–ImageView不会显示用相机拍摄的照片,第1张

概述请耐心等待……我一直在寻找DAYS,寻找能够启动相机活动,拍摄照片并将其置于简单的ImageView>上的简单代码.下面发布的代码会激活活动并拍摄照片,但图像不显示在ImageView上!究竟缺少什么?:'(publicclassMainActivityextendsActivity{privatestaticfinalintPICK_IMAGE=0

请耐心等待……我一直在寻找DAYS,寻找能够启动相机活动,拍摄照片并将其置于简单的ImageVIEw>上的简单代码.下面发布的代码会激活活动并拍摄照片,但图像不显示在ImageVIEw上!究竟缺少什么? :'(

public class MainActivity extends Activity{private static final int PICK_IMAGE = 0;private static final int PICK_IMAGE_FROM_galLERY = 1;private button mBtnCamera, mBtngallery, mBtnCancel;private ImageVIEw mImageVIEw;private Uri mURI;private String mPhotopath;@OverrIDeprotected voID onCreate(Bundle savedInstanceState){    super.onCreate(savedInstanceState);    setContentVIEw(R.layout.activity_main);    mImageVIEw = (ImageVIEw) findVIEwByID(R.ID.imgdisplayImage);    mBtnCamera = (button) findVIEwByID(R.ID.btnPhotoCamera);    mBtngallery = (button) findVIEwByID(R.ID.btnPhotogallery);    mBtnCancel = (button) findVIEwByID(R.ID.btnCancel);    mBtnCamera.setonClickListener(new OnClickListener()    {        @OverrIDe        public voID onClick(VIEw v)        {            Intent camera = new Intent();            camera.setAction(MediaStore.ACTION_IMAGE_CAPTURE);            camera.putExtra("crop", "true");            file f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);            mURI = Uri.fromfile(new file(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myfile.jpg"));            camera.putExtra(MediaStore.EXTRA_OUTPUT, mURI);            startActivityForResult(camera, PICK_IMAGE);        }    });}   @OverrIDeprotected voID onActivityResult(int requestCode, int resultCode, Intent data){    if (requestCode == PICK_IMAGE)    {        // Result includes a Bitmap thumbnail?        if (data != null)        {            if (data.hasExtra("data"))            {                //Bitmap thumbnail = data.getParcelableExtra("data");                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");                mImageVIEw.setimageBitmap(thumbnail);            }        }        // If there is no thumbnail data, the image will have been stored in target output URI.        else        {            Cursor cursor = getContentResolver().query(                    Media.EXTERNAL_CONTENT_URI, new String[]                            {                                Media.DATA,                                 Media.DATE_ADDED,                                 MediaStore.Images.ImageColumns.ORIENTATION                            },                             Media.DATE_ADDED,                             null,                             "date_added ASC"            );            if (cursor != null && cursor.movetoFirst())            {                do                {                    mURI = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));                    mPhotopath = mURI.toString();                }                while (cursor.movetoNext());                cursor.close();            }            // Resize full image to fit out in image vIEw.            int wIDth = mImageVIEw.getWIDth();            int height = mImageVIEw.getHeight();            BitmapFactory.Options factoryOptions = new BitmapFactory.Options();            factoryOptions.inJustDecodeBounds = true;            BitmapFactory.decodefile(/*mURI.getPath()*/ mPhotopath, factoryOptions);            int imageWIDth = factoryOptions.outWIDth;            int imageHeight = factoryOptions.outHeight;            // Determine how much to scale down the image            int scaleFactor = Math.min(                    imageWIDth/wIDth,                    imageHeight/height                    );            // Decode the image file into a Bitmap sized to fill vIEw            factoryOptions.inJustDecodeBounds = false;            factoryOptions.inSampleSize = scaleFactor;            factoryOptions.inPurgeable = true;            Bitmap bitmap = BitmapFactory.decodefile(/*mURI.getPath()*/ mPhotopath, factoryOptions);            mImageVIEw.setimageBitmap(bitmap);        }    }}}

解决方法:

I had same problem in some devices of samsung androID Then I implemented logic to get path of captured photo.

@OverrIDeprotected voID onActivityResult(int requestCode, int resultCode, Intent data){    if (requestCode == PICK_IMAGE)    {        Intent cameraIntent = new Intent(androID.provIDer.MediaStore.ACTION_IMAGE_CAPTURE);         startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);        Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, new String[]{Media.DATA, Media.DATE_ADDED, MediaStore.Images.ImageColumns.ORIENTATION}, Media.DATE_ADDED, null, "date_added ASC");        if(cursor != null && cursor.movetoFirst())        {            do {                uri = Uri.parse(cursor.getString(cursor.getColumnIndex(Media.DATA)));                photopath = uri.toString();            }while(cursor.movetoNext());            cursor.close();        }        if(photopath != null) {            Bitmap bitmap = BitmapFactory.decodefile(photopath);            ///Do Implement your logic whatever you want.            mImageVIEw.setimageBitmap(bitmap);        }    }}

总结

以上是内存溢出为你收集整理的Android – ImageView不会显示用相机拍摄的照片全部内容,希望文章能够帮你解决Android – ImageView不会显示用相机拍摄的照片所遇到的程序开发问题。

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存