android-仅在Samsung Galaxy S4上运行时,ImageView无法通过setImageURI加载

android-仅在Samsung Galaxy S4上运行时,ImageView无法通过setImageURI加载,第1张

概述在SamsungGalaxyS4(型号:GT-I9500)上运行一些基本代码时,我遇到了一个特定问题.我当时是通过相机或图库实现图像选择器的,但我一生都无法弄清楚为什么在调用时ImageView是空白的-imageView.setImageURI(uri);直到我在模拟器(然后是Nexus5)中运行完全相同的代码后,我才发现这是

在Samsung galaxy S4(型号:GT-I9500)上运行一些基本代码时,我遇到了一个特定问题.@H_502_1@

我当时是通过相机或图库实现图像选择器的,但我一生都无法弄清楚为什么在调用时ImageVIEw是空白的-@H_502_1@

imageVIEw.setimageURI(uri);@H_502_1@

直到我在模拟器(然后是Nexus 5)中运行完全相同的代码后,我才发现这是Samsung S4问题.@H_502_1@

完整的示例项目可以在Github & ready to run上找到@H_502_1@

我使用的代码来自此SO post:@H_502_1@

在OnCreate中:@H_502_1@

@H_502_1@

btn.setonClickListener(new VIEw.OnClickListener() {        @OverrIDe        public voID onClick(VIEw v) {            AlertDialog.Builder builder = new AlertDialog.Builder(context);            builder.setTitle("Choose Image Source");            builder.setItems(new CharSequence[]{"gallery", "Camera"},                    new DialogInterface.OnClickListener() {                        @OverrIDe                        public voID onClick(DialogInterface dialog, int which) {                            switch (which) {                                case 0:                                    //Launching the gallery                                    Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);                                    startActivityForResult(i, galLERY);                                    break;                                case 1:                                    //Specify a camera intent                                    Intent getCameraimage = new Intent("androID.media.action.IMAGE_CAPTURE");                                    file cameraFolder;                                    //Check to see if there is an SD card mounted                                    if (androID.os.Environment.getExternalStorageState().equals                                            (androID.os.Environment.MEDIA_MOUNTED))                                        cameraFolder = new file(androID.os.Environment.getExternalStorageDirectory(),                                                IMAGEFolDER);                                    else                                        cameraFolder = MainActivity.this.getCacheDir();                                    if (!cameraFolder.exists())                                        cameraFolder.mkdirs();                                    //Appending timestamp to "picture_"                                    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");                                    String timeStamp = dateFormat.format(new Date());                                    String imagefilename = "picture_" + timeStamp + ".jpg";                                    file photo = new file(Environment.getExternalStorageDirectory(),                                            IMAGEFolDER + imagefilename);                                    getCameraimage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromfile(photo));                                    //Setting a global variable to be used in the OnActivityResult                                    imageURI = Uri.fromfile(photo);                                    startActivityForResult(getCameraimage, CAMERA);                                    break;                                default:                                    break;                            }                        }                    });            builder.show();        }    });

OnActivityResult:@H_502_1@

@H_502_1@

protected voID onActivityResult(int requestCode, int resultCode, Intent data) {    super.onActivityResult(requestCode, resultCode, data);    if (resultCode == RESulT_OK) {        switch (requestCode) {            case galLERY:                Uri selectedImage = data.getData();                imageVIEw.setimageURI(selectedImage);                break;            case CAMERA:                imageVIEw.setimageURI(imageURI);                break;        }    }}

使用Picasso时也会发生@H_502_1@

@H_502_1@

 if (resultCode == RESulT_OK) {        switch (requestCode) {            case galLERY:                Uri selectedImage = data.getData();                Picasso.with(context)                        .load(selectedImage)                        .into(imageVIEw);                break;            case CAMERA:                Picasso.with(context)                        .load(imageURI)                        .into(imageVIEw);                break;        }    }

使用位图工厂时也会发生@H_502_1@

@H_502_1@

  try {                    Bitmap bitmap = BitmapFactory.decodeStream(context.getContentResolver().openinputStream(imageURI));                    imageVIEw.setimageBitmap(bitmap);                } catch (fileNotFoundException e) {                    e.printstacktrace();                }

在运行4.2.2的Samsung S4上运行时的结果@H_502_1@

在运行AndroID 4.4.4的GenyMotion 2.4.0上运行时的结果@H_502_1@

有人知道为什么会这样吗?@H_502_1@

解决方法:@H_502_1@

因此问题出在三星S4无法处理的图像位图太大.@H_502_1@

令人沮丧的是,不会引发任何错误-正确的解决方案如下:@H_502_1@

@H_502_1@

switch (requestCode) {            case galLERY:                Bitmap bitmap = createScaledBitmap(getimagePath(data, getApplicationContext()), imageVIEw.getWIDth(), imageVIEw.getHeight());                imageVIEw.setimageBitmap(bitmap);                break;            case CAMERA:                String path = imageURI.getPath();                Bitmap bitmapCamera = createScaledBitmap(path, imageVIEw.getWIDth(), imageVIEw.getHeight());                imageVIEw.setimageBitmap(bitmapCamera);                break;        }

辅助方法:@H_502_1@

@H_502_1@

// Function to get image path from ImagePickerpublic static String getimagePath(Intent data, Context context) {    Uri selectedImage = data.getData();    String[] filePathColumn = {MediaStore.Images.Media.DATA};    Cursor cursor = context.getContentResolver().query(selectedImage, filePathColumn, null, null, null);    cursor.movetoFirst();    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);    String picturePath = cursor.getString(columnIndex);    cursor.close();    return picturePath;}public Bitmap createScaledBitmap(String pathname, int wIDth, int height) {    final BitmapFactory.Options opt = new BitmapFactory.Options();    opt.inJustDecodeBounds = true;    BitmapFactory.decodefile(pathname, opt);    opt.inSampleSize = calculateBmpSampleSize(opt, wIDth, height);    opt.inJustDecodeBounds = false;    return BitmapFactory.decodefile(pathname, opt);}public int calculateBmpSampleSize(BitmapFactory.Options opt, int wIDth, int height) {    final int outHeight = opt.outHeight;    final int outWIDth = opt.outWIDth;    int sampleSize = 1;    if (outHeight > height || outWIDth > wIDth) {        final int heightRatio = Math.round((float) outHeight / (float) height);        final int wIDthRatio = Math.round((float) outWIDth / (float) wIDth);        sampleSize = heightRatio < wIDthRatio ? heightRatio : wIDthRatio;    }    return sampleSize;}
总结

以上是内存溢出为你收集整理的android-仅在Samsung Galaxy S4上运行时,ImageView无法通过setImageURI加载全部内容,希望文章能够帮你解决android-仅在Samsung Galaxy S4上运行时,ImageView无法通过setImageURI加载所遇到的程序开发问题。

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

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

原文地址: http://outofmemory.cn/web/1088889.html

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

发表评论

登录后才能评论

评论列表(0条)

保存