Возможность сохранить изображение в шаблоне, но невозможно получить к нему доступ с помощью «Файла» для создания эскизов.

Я могу сохранить изображение и отобразить его ImageView, используя тот жеfilePathно получить сообщение об ошибке при попытке доступа к нему для создания эскизов с помощьюThumbnailUtils

У меня есть фрагмент, который захватывает изображение, используяActivityResultContract

      class TakePictureWithUriReturnContract : ActivityResultContract<Uri, Pair<Boolean, Uri>>() {
    private lateinit var imageUri: Uri

    @CallSuper
    override fun createIntent(context: Context, input: Uri): Intent {
        imageUri = input
        return Intent(MediaStore.ACTION_IMAGE_CAPTURE).putExtra(MediaStore.EXTRA_OUTPUT, input)
    }

    override fun getSynchronousResult(
        context: Context,
        input: Uri
    ): SynchronousResult<Pair<Boolean, Uri>>? = null

    @Suppress("AutoBoxing")
    override fun parseResult(resultCode: Int, intent: Intent?): Pair<Boolean, Uri> {
        return (resultCode == Activity.RESULT_OK) to imageUri
    }
}

Вышеупомянутый код используется для обработки контракта

      val photoFile = File.createTempFile(
            "IMG_",
            ".jpg",
            requireContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        )

        val uri = FileProvider.getUriForFile(
            requireContext(),
            "${requireContext().packageName}.fileprovider",
            photoFile
        )

        sampleActivityRes.launch(uri)

Этот метод используется для запуска контракта, и я могу успешно получить URI.

Я также могу просматривать изображение, установив его наImageView.

Но когда я пытаюсь получить к нему доступ для создания миниатюр, используя

      private fun generateImageThumbnail(imagePath: String): Bitmap {
        val THUMB_SIZE = 64

        val cancellationToken = CancellationSignal()


        return ThumbnailUtils.createImageThumbnail(
            File(imagePath),
            Size(THUMB_SIZE, THUMB_SIZE),
            cancellationToken
        )
    }

я получилjava.io.FileNotFoundExceptionисключение.

Мой файл_paths.xml

      <?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-path
        name="my_images"
        path="Android/data/com.example.name/files/Pictures" />
    <external-path
        name="external"
        path="." />
    <external-files-path
        name="external_files"
        path="." />
    <cache-path
        name="cache"
        path="." />
    <external-cache-path
        name="external_cache"
        path="." />
    <files-path
        name="files"
        path="." />
    <files-path
        name="images"
        path="." />
</paths>

Манифест Android

      <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature android:name="android.hardware.camera.any" />
    <uses-feature android:name="android.hardware.location.gps" />
    <uses-feature android:name="android.hardware.location.network" />

    <application
        android:name=".TripMemoryApp"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:grantUriPermissions="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:requestLegacyExternalStorage="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.TripMemory"
        tools:targetApi="31">

        <provider
            android:name="com.android.tripmemory.utils.MyFileProvider"
            android:authorities="com.android.tripmemory.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths" />
        </provider>
        <!--
             TODO: Before you run your application, you need a Google Maps API key.

             To get one, follow the directions here:

                https://developers.google.com/maps/documentation/android-sdk/get-api-key

             Once you have your API key (it starts with "AIza"), define a new property in your
             project's local.properties file (e.g. MAPS_API_KEY=Aiza...), and replace the
             "YOUR_API_KEY" string in this file with "${MAPS_API_KEY}".
        -->
        <meta-data
            android:name="com.google.android.geo.API_KEY"
            android:value="AIzaSyAriNq0SqdYMLgBkShF7E0xaPrGpMf4YZ8" />

        <activity
            android:name=".ui.MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

образецResActivity

      val sampleActivityRes = registerForActivityResult(
        TakePictureWithUriReturnContract()
    ) { (isSuccess, imageURI) ->
        if (isSuccess) {
            Log.d(LOG_TAG, "Image URI: $imageURI")
            binding.sampleImage.bringToFront()
            binding.sampleImage.setImageURI(imageURI)

            navigateToImagePreview(
                imageURI = imageURI.toString(),
                pathId = viewModel.pathInstance.id
            )
        }
    }

1 ответ

Благодаря @blackapps

Проблема заключалась в том, чтоimagePathвgenerateImageThumbnailбылSchemaвместо того, чтобы бытьURI/path.

Это сработает, если мы пройдемabsolutePathфайла

поэтому изменение будет звонитьgenerateImageThumbnail(photoFile.absolutePath)с абсолютным путем к файлу

      val photoFile = File.createTempFile(
            "IMG_",
            ".jpg",
            requireContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        )

generateImageThumbnail(photoFile.absolutePath)
Другие вопросы по тегам