图像未在Imageview中显示

时间:2017-06-28 11:29:39

标签: android android-layout android-fragments

从自定义相机拍摄的图像在三星S4的荣耀7手机的imageview中没有显示它工作正常。即使图像在SD卡中保存也是在图像视图中也没有显示。< / p>

保存图像功能

File imageFile;
        // convert byte array into bitmap
        Bitmap loadedImage = null;
        loadedImage = BitmapFactory.decodeByteArray(data, 0,
                data.length);

        // rotate Image
        Matrix rotateMatrix = new Matrix();
        rotateMatrix.postRotate(rotation);
        Bitmap rotatedBitmap = Bitmap.createBitmap(loadedImage, 0,
                0, loadedImage.getWidth(), loadedImage.getHeight(),
                rotateMatrix, false);

        String state = Environment.getExternalStorageState();
        File folder = null;
        if (state.contains(Environment.MEDIA_MOUNTED)) {
            folder = new File(Environment
                    .getExternalStorageDirectory() + "/Image");
        } else {
            folder = new File(Environment
                    .getExternalStorageDirectory() + "/Image");
        }

        boolean success = true;
        if (!folder.exists()) {
            success = folder.mkdirs();
        }
        if (success) {
            java.util.Date date = new java.util.Date();
            imageFile = new File(folder.getAbsolutePath()
                    + File.separator
                    + new Timestamp(date.getTime()).toString()
                    + "Icon.jpg");

            imageFile.createNewFile();
        } else {
            Toast.makeText(mContext, "Image Not saved",
                    Toast.LENGTH_SHORT).show();
            return;
        }
        ByteArrayOutputStream ostream = new ByteArrayOutputStream();
        // save image into gallery
        rotatedBitmap.compress(CompressFormat.JPEG, 50, ostream);

        FileOutputStream fout = new FileOutputStream(imageFile);
        fout.write(ostream.toByteArray());
        fout.close();
        ContentValues values = new ContentValues();

        values.put(Images.Media.DATE_TAKEN,
                System.currentTimeMillis());
        values.put(Images.Media.MIME_TYPE, "image/jpeg");
        values.put(MediaStore.MediaColumns.DATA,
                imageFile.getAbsolutePath());

        mContext.getContentResolver().insert(
                Images.Media.EXTERNAL_CONTENT_URI, values);

        mImagedata = null;

        String filepath = imageFile.getAbsolutePath();
        pref.saveString(Constant.PrefConstants.KEY_USER_IMAGE_PATH, filepath);

显示图像

File imgFile = new File(pref.getString(Constant.PrefConstants.KEY_USER_IMAGE_PATH));
ivProfileImage.setImageURI(Uri.parse(imgFile.getAbsolutePath()));

2 个答案:

答案 0 :(得分:0)

这很可能是因为照片的大小。检查logcat是否有警告。

解决方案:https://stackoverflow.com/a/17125888/2176401

答案 1 :(得分:0)

从SD卡中检索图像添加运行时间权限,即 READ_EXTERNAL_STORAGE如果Android版本&gt; 23