I am getting this msg speech that did not find (No

2019-09-19 18:25发布

问题:

I am getting this msg speech that did not find (No such file or directory) the path is correct what could be wrong?

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

LOG

   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 line error 274

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

path

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

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

manifest

  <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>

method to capture the gallery image

 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:

As the exception indicates, The path does not exist!!

As a bit of advice, try not to fight with exceptions, Just follow them, even if you're pretty sure!

Anyway this time its the

val authorities = activity.packageName + ".fileprovider"

part to blame because you have defined the provider in manifest with .provider suffix.

So change to:

val authorities = activity.packageName + ".provider"

It should be working by that change.

Cheers!