当我拍照时,它仅在Galaxy s4中不显示在imageView上

时间:2016-06-23 10:14:57

标签: android

在我的应用中,我必须让用户从相机拍照/从图库中选择。 我的代码在某些设备上工作正常,但是当我在Galaxy s4上运行并通过相机拍照时,照片没有显示在imageView上(从图库中选择 - 正常工作)问题就是在Galaxy s4中通过相机拍照时。 我在将图片显示在imageView上之前缩放了图片。 这是我拍摄照片的代码:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                        File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                        startActivityForResult(intent, 1);

拍照后这是我的代码:

 if (requestCode == 1) 
                    { 
                        File f = new File(Environment.getExternalStorageDirectory().toString());
                        File image_file = new File(Environment.getExternalStorageDirectory().toString());
                        for (File temp : f.listFiles())
                        {
                            if (temp.getName().equals("temp.jpg"))
                            {
                                f = temp;                           
                                break;
                            }
                        }
                        try 
                        {
                            Bitmap bitmap;
                            //BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                            //bitmap=decodeFile(f.getAbsolutePath());//Collapse image                         
                            //setImageOnBitmap(bitmap);//Set the image on the imageview



                            BitmapHandler b=new BitmapHandler(getApplicationContext());
                            bitmap=b.decodeFileAsPath(f.getAbsolutePath(),"camera");
                            setImageOnBitmap(bitmap);




                            String path = android.os.Environment
                                    .getExternalStorageDirectory()
                                    + File.separator
                                    + "Phoenix" + File.separator + "default";
                            f.delete();                       
                        } 
                        catch (Exception e) 
                        {
                            e.printStackTrace();
                        }
                    } 

这是我解码图像文件的功能:

  public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) 
{ // BEST QUALITY MATCH

    //First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize, Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    int inSampleSize = 1;

    if (height > reqHeight) 
    {
        inSampleSize = Math.round((float)height / (float)reqHeight);
    }
    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) 
    {
        //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
        inSampleSize = Math.round((float)width / (float)reqWidth);
    }

    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
}

1 个答案:

答案 0 :(得分:0)

我在S4上有类似的问题,这就是我解决的问题:

mImageView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

希望这有帮助。