找不到类“ android.provider.MediaStore $ Downloads”

问题描述 投票:0回答:1

我在how to save bitmap to android gallery使用包雷给出的名为private fun saveImage的代码保存位图

为了解决Kotlin中已过时的问题,我删除了以下几行:

 //values.put(MediaStore.Images.Media.DATA, file.absolutePath) 
//context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)

并添加此:

 val contentValues = ContentValues()

 contentValues.put(MediaStore.Images.Media.RELATIVE_PATH, folderName)

 this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)

然后,我将函数(在DidtodayActivity中称为:)>

saveImage(bitmapImg, this.applicationContext)

图像已下载到Phone> Android> Data> com.example.MyApp1> files> MyFiles

。我可以在设备上看到下载的图像。

但是,由于此行上的运行时错误,活动文件突然关闭(并且activity_main

。xml直接打开:
this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)

原因

java.lang.ClassNotFoundException: Didn't find class "android.provider.MediaStore$Downloads" on path: DexPathList[[zip file "/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/lib/arm64, /data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk!/lib/arm64-v8a, /system/lib64]]

Detailmessage:

]
**Didn't find class "android.provider.MediaStore$Downloads"** on path: DexPathList[[zip file "/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/lib/arm64, /data/app/com.example.MyApp1-rvQQjSAj4NilLch2h12faQ==/base.apk!/lib/arm64-v8a, /system/lib64]]

失败的解决方案:Landroid / provider / MediaStore $ Downloads;

模块级别build.gradle为:

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.2"

    defaultConfig {
        applicationId "com.example.MyApp1"
        minSdkVersion 16
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

        multiDexEnabled true

    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }

    aaptOptions{
        noCompress "tflite"
        noCompress "lite"
    }
}

此外,以下代码已添加到build.gradle文件的依赖项中:

 implementation "com.android.support:multidex:2.0.1"

并且,我将此代码添加到清单文件中:

  android:name="androidx.multidex.MultiDexApplication">

如何解决运行时错误问题?

我尝试过这个;

this.applicationContext.contentResolver.insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)

代替此;

this.applicationContext.contentResolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues)

但是,它再次由于运行时错误而崩溃;

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=203, result=-1, data=Intent { (has extras) }} to 
activity {com.example.MyApp1/com.example.MyApp1.DidtodayActivity}: java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri 
content://media/external/images/media from pid=3812, uid=10270 
requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()

但是,清单文件中已经包含以下代码;

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

因此,是否还有其他替代方法可以代替MediaStore.Downloads

我正在使用包雷给出的名称为private fun saveImage的代码,以了解如何将位图保存到android画廊中以保存位图。为解决Kotlin中已过时的问题,我删除了以下几行:// ...

android kotlin mediastore android-multidex
1个回答
0
投票

在Android 5.0之后,默认情况下启用了多义处理。

© www.soinside.com 2019 - 2024. All rights reserved.