FileProvider,Bitmap报错 java.io.FileNotFoundException

java.io.FileNotFoundException: /my_images/Android/data/com.paning.safeline/cache/image/1573195780616.jpg (No such file or directory)   问题请教

今天出现一个生产问题,通过FileProvider.getUriForFile得到uri,然后通过uri.getPath()获取到文件路径,但这边上传前需压缩图片,BitmapFactory.decodeFile(filePath, options)却报错FileNotFoundException,个人感觉很奇怪,还请大佬们不吝赐教,谢谢!!

以下为代码,红色是具体方法调用,蓝色是debug的打印信息

/** 
* AndroidManifest.xml 
*/
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.paning.safeline.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths" />
</provider>

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="." />
</paths>
/**
 * 拍照
 */
public static void camera() {
    Intent getImageByCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {//7.0以上要通过FileProvider将File转化为Uri
        savePath = FileProvider.getUriForFile(context, context.getPackageName()+".fileprovider",
                createCaptureFile(this.context, cameraParam.getType(), System.currentTimeMillis() + ""));
    } else {//7.0以下则直接使用Uri的fromFile方法将File转化为Uri
        savePath = Uri.fromFile(createCaptureFile(context, cameraParam.getType(), System.currentTimeMillis() + ""));
    }
    getImageByCamera.putExtra(MediaStore.EXTRA_OUTPUT, savePath);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        getImageByCamera.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        getImageByCamera.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }
    activity.startActivityForResult(getImageByCamera, request_code);
}

/** 
* 拍照文件路径 
*/
File createCaptureFile(Context context, String encodingType, String fileName) {
        if (fileName.isEmpty()) {
            fileName = ".Pic";
        }
        if ("jpg".equals(encodingType)) {
            fileName = fileName + ".jpg";
        } else if ("png".equals(encodingType)) {
            fileName = fileName + ".png";
        } else {
            fileName = fileName + ".jpg";
        }
        String filePath ;
        filePath = FileUtil.getSDAppCachePath(context) + File.separator + "image";
        File file = new File(filePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        return new File(filePath, fileName);
    }
public void onActivityResult(int requestCode, int resultCode, Intent data) {
        String imgPath;
        switch (requestCode) {
            case REQUEST_CAMERA:
                if (resultCode == 0) {
                    executeCallback(1, new AladdinErrorMessage("0", "取消拍照", null, null), null);
                } else if (resultCode == Activity.RESULT_OK && this.cameraParam != null && this.imageUri != null) {
                    imgPath = this.imageUri.getPath();
imageUri:"content://com.paning.safeline.fileprovider/my_images/Android/data/com.paning.safeline/cache/image/1573190714806.jpg"
                    Log.i(TAG, "imgPath;" + imgPath);
imgPath:"/my_images/Android/data/com.paning.safeline/cache/image/1573190714806.jpg"
                    if (this.cameraParam.isEdit()) { false
                        Uri orgUri = Uri.fromFile(new File(imgPath));
                        CameraHelper.cropCamera(this.context, orgUri, this.imageUri, this.cameraParam.getWidth(), this.cameraParam.getHeight(), REQUEST_CORP);
                    } else {
                        this.callBackValue(imgPath);
                    }
                }
                break;
private void callBackValue(final String imgPath) {
  JSONObject jsonObject = new JSONObject();
  final File tempFile = createCaptureFile(context, cameraParam.getType(), System.currentTimeMillis() + "");
  String path = compressImageToPath(imgPath, tempFile, cameraParam.getWidth(),          cameraParam.getMaxFileSize() == 0 ? (int) 1.5 * 1024 : cameraParam.getMaxFileSize(),cameraParam.getType());
  File file = new File(path);
  jsonObject.put("uri", path);
  executeCallback(0, null, jsonObject.toString());
}
String compressImageToPath(String imgPath, File tempFile, int width, int maxFileSize, String type) {
imgPath:"/my_images/Android/data/com.paning.safeline/cache/image/1573190714806.jpg"
tempFile:"/storage/emulated/0/Android/data/com.paning.safeline/cache/image/1573190795155.jpg"
    FileOutputStream fileOutputStream = null;
    try {
        Bitmap bitmap = BitmapUtils.compressBitMapBySise(imgPath, width);bitmap:"null"
        if (cameraParam.getSource() == 1) {
            //旋转90度
            bitmap = rotaingImageView(90, bitmap);
        }
        fileOutputStream = new FileOutputStream(tempFile);
        Bitmap.CompressFormat compressFormat = "jpg".equals(type) ? Bitmap.CompressFormat.JPEG : Bitmap.CompressFormat.PNG;
        int quality = 100;
        if (bitmap.compress(compressFormat, quality, fileOutputStream)) {
            fileOutputStream.flush();
            fileOutputStream.close();
            long fileSize = tempFile.length();
            while (fileSize / 1024 > maxFileSize) {
                quality -= 10;
                fileOutputStream = new FileOutputStream(tempFile, false);
                bitmap.compress(compressFormat, quality, fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
                fileSize = tempFile.length();
                if (quality <= 0) break;
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fileOutputStream != null) {
            try {
                fileOutputStream.flush();
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return tempFile.getAbsolutePath();
}
/**
* 拍照文件压缩
*/
public static Bitmap compressBitMapBySise(String filePath, int width) {
        Bitmap mBitmap = null;
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);
        // 调用上面定义的方法计算inSampleSize值
        try {
            options.inSampleSize = calculateSampleSize(options, getScreenWidth(SafeLineAppliaction.getInstance()), getScreenHeight(SafeLineAppliaction.getInstance()));
        } catch (Exception e) {
            e.printStackTrace();
            options.inSampleSize = 2;
        }
        if (options.inSampleSize >= 3) {
            options.inSampleSize = 3;
        }
        // 使用获取到的inSampleSize值再次解析图片
        options.inJustDecodeBounds = false;
        mBitmap = BitmapFactory.decodeFile(filePath, options)
        return mBitmap;
    }

/**
* BitmapFactory
*/
public static Bitmap decodeFile(String pathName, Options opts) {
    validate(opts);
    Bitmap bm = null;
    InputStream stream = null;
    try {
        stream = new FileInputStream(pathName);
        bm = decodeStream(stream, null, opts);
    } catch (Exception e) {
        /*  do nothing.
            If the exception happened on open, bm will be null.
        */
        Log.e("BitmapFactory", "Unable to decode stream: " + e);
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) { e:"java.io.FileNotFoundException: /my_images/Android/data/com.paning.safeline/cache/image/1573190714806.jpg (No such file or directory)"
                // do nothing here
            }
        }
    }
    return bm;
}

 

网上看了很多,也尝试却依然报错,例如这种

https://blog.csdn.net/xuewater/article/details/24803457?utm_source=blogxgwz8

也没有解决问题,但是把之前代码改成了如下内容,却能正常执行,十分纳闷,求教,为什么uri.getPath()获取到文件路径不能正常得到bitmap?

//-s- No such file or directory (@xml/file_paths  my_images )
//String path = compressImageToPath(imgPath, tempFile, cameraParam.getWidth(), cameraParam.getMaxFileSize() == 0 ? (int) 1.5 * 1024 : cameraParam.getMaxFileSize(), cameraParam.getType());
String thisImgPath=imgPath;
if (imgPath.contains("my_images")){
    String[] mPath =imgPath.split("my_images");
    thisImgPath = Environment.getExternalStorageDirectory()+mPath[1] ;
thisImgPath:"/storage/emulated/0/Android/data/com.paning.safeline/cache/image/1573190714806.jpg"
}
String path = compressImageToPath(thisImgPath, tempFile, cameraParam.getWidth(), cameraParam.getMaxFileSize() == 0 ? (int) 1.5 * 1024 : cameraParam.getMaxFileSize(), cameraParam.getType());
//-e-
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值