我收到此找不到的味精语音(没有此类文件或目录)

时间:2019-05-07 18:14:49

标签: android kotlin bitmap

我收到的此味精语音未找到(没有这样的文件或目录)路径正确,这可能是错误的吗?

W/System.err: java.io.FileNotFoundException: /map.app.fileprovider/files/Pictures/JPEG_20190507_180502_4494360846236185755.jpg (No such file or directory)

日志

   W/System.err: java.io.FileNotFoundException: /map.app.fileprovider/files/Pictures/JPEG_20190507_180502_4494360846236185755.jpg (No such file or directory)
    W/System.err:     at java.io.FileInputStream.open0(Native Method)
            at java.io.FileInputStream.open(FileInputStream.java:200)
    W/System.err:     at java.io.FileInputStream.<init>(FileInputStream.java:150)
            at java.io.FileInputStream.<init>(FileInputStream.java:103)
    W/System.err:     at android.content.ContentResolver.openInputStream(ContentResolver.java:965)
            at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:888)
    W/System.err:     at map.app.fragments.ReportFragment.onActivityResult(ReportFragment.kt:274)

ReportFragmnet行错误274

val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, Uri.parse("file://"+this.imageUri!!)) as Bitmap

路径

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path

        name="files"
        path="."/>
</paths>

清单

  <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/paths" />
        </provider>

捕获图库图像的方法

 fun openCamera() {
    try {
        val imageFile = createImageFile()
        val callCameraIntent =  Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        if(callCameraIntent.resolveActivity(activity.packageManager) != null) {
            val authorities = activity.packageName + ".fileprovider"
            this.imageUri = FileProvider.getUriForFile(context, authorities, imageFile)
            callCameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
            startActivityForResult(callCameraIntent, IMAGE_PICK_CODE)
        }
    } catch (e: IOException) {
        Toast.makeText(context, "Could not create file!", Toast.LENGTH_SHORT).show()
    }
}

onActivityResult

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == IMAGE_PICK_CODE && resultCode == RESULT_OK) {


            try {

                //Getting the Bitmap from Gallery
                val bitmap = MediaStore.Images.Media.getBitmap(context.contentResolver, Uri.parse("file://"+this.imageUri!!)) as Bitmap
                this.imgThumb!!.setImageBitmap(bitmap)
                this.pictureTaken = true
            } catch (e:IOException) {
                e.printStackTrace()
            }
        } else {
            Toast.makeText(context, "Error loading image", Toast.LENGTH_LONG)
        }
    }

    @Throws(IOException::class)
    fun createImageFile(): File {
        val timeStamp: String = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val imageFileName: String = "JPEG_" + timeStamp + "_"
        val storageDir: File = activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        if(!storageDir.exists()) storageDir.mkdirs()
        val imageFile = File.createTempFile(imageFileName, ".jpg", storageDir)
        imageFilePath = imageFile.absolutePath
        return imageFile
    }

1 个答案:

答案 0 :(得分:0)

如异常所示,该路径不存在!

作为一些建议,请尝试与异常作斗争,即使您确定,也要遵循它们!

无论如何这次是

val authorities = activity.packageName + ".fileprovider"

值得指责,因为您已在清单中以.provider后缀定义了提供程序。

因此更改为:

val authorities = activity.packageName + ".provider"

它应该通过更改而起作用。

干杯!