我在我的Android应用中集成了Snapchat的Creative Kit。处理后,我从服务器收到了字节数组形式的图像,将其保存到磁盘,然后将文件发送到Snapchat的Creative Kit,如下所示。
class QnADialog extends ComponentDialog{
constructor(id){
super(id||QNA_DIALOG);
this.addDialog(new TextPrompt(ASK_QUERY_PROMPT))
this.addDialog(new WaterfallDialog(WATERFALL_DIALOG, [
this.initialStep.bind(this),
this.callQnAMakerStep.bind(this)
]));
this.initialDialogId = WATERFALL_DIALOG;
try {
this.qnaMaker = new QnAMaker({
knowledgeBaseId: process.env.QnAKnowledgebaseId,
endpointKey: process.env.QnAAuthKey,
host: process.env.QnAEndpointHostName
});
} catch (err) {
console.log(`QnAMaker Exception: ${ err } Check your QnAMaker configuration in .env`);
}
}
async initialStep(step){
console.log("QnADialog.initialStep :")
await step.context.sendActivity("Hello testing!");
return step.prompt(ASK_QUERY_PROMPT, 'Ask any query you have...');
}
async callQnAMakerStep(step){
var query = step.result;
console.log("qnaDialog.callQnAMakerStep calling QnA Maker...");
const qnaResults = await this.qnaMaker.getAnswers(step.context);
console.log("qnaResult is : "+ qnaResult);
// If an answer was received from QnA Maker, send the answer back to the user.
if (qnaResults[0]) {
await step.context.sendActivity(qnaResults[0].answer);
// If no answers were returned from QnA Maker, reply with help.
} else {
await step.context.sendActivity('No QnA Maker answers were found.');
}
return await step.endDialog();
}
}
我还在清单文件中添加了 private fun downloadImage(
fileName: String,
imageByteArray: ByteArray?): Uri? {
val state = Environment.getExternalStorageState()
if (Environment.MEDIA_MOUNTED == state) {
val downloadDir = File(
Environment.getExternalStorageDirectory(), context?.getString(R.string.app_name)
)
if (!downloadDir.isDirectory) {
downloadDir.mkdirs()
}
val file = File(downloadDir, fileName)
var ostream: FileOutputStream? = null
try {
ostream = FileOutputStream(file)
ostream.write(imageByteArray)
ostream.flush()
ostream.close()
}
} catch (e: IOException) {
e.printStackTrace()
}
val snapCreativeKitApi = SnapCreative.getApi(context!!)
val snapMediaFactory = SnapCreative.getMediaFactory(context!!)
lateinit var snapPhotoFile: SnapPhotoFile
try {
snapPhotoFile = snapMediaFactory.getSnapPhotoFromFile(file)
} catch (e: SnapMediaSizeException) {
return
}
val snapPhotoContent = SnapPhotoContent(snapPhotoFile)
snapCreativeKitApi.send(snapPhotoContent)
}
}
,如下所示:
provider
在 <provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths_app" />
</provider>
中,我通过引用this答案尝试了所有可能的路径,但没有一个起作用。
provider_paths_app.xml
使用上面的路径,我得到下面的错误。
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="My App Name"
path="." />
</paths>
我要做的就是将此图像发送给Snapchat,但我无法弄清楚自己在做什么错。任何帮助将不胜感激。
答案 0 :(得分:6)
就我而言。
我有一个图书馆项目。让我们称之为:LibApk
当我编写此代码时:build.grale(app)中的applicationId“ my.package.name”
在构建gradle时告诉我: 图书馆项目无法设置applicationId。
在默认配置中,applicationId设置为'com.darcy.apkupdate'。
因此,我删除了applicationId“ my.package.name”。
但是我忘了更新使用 $ {applicationId}
的AndroidManifest.xml文件。这是问题所在!
此后,对我来说真是不可思议!
希望这对您有帮助...
答案 1 :(得分:5)
首先在清单中的标签下写下以下标签
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
然后在res中创建一个xml文件夹并创建一个文件名:Provide + paths.xml 然后复制粘贴代码:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path
name="external_files"
path="." />
</paths>
,现在大多数开发人员都在程序中错误地在程序中创建File,然后我们将使用:
FileProvider.getUriForFile(Objects.requireNonNull(getApplicationContext()),
BuildConfig.APPLICATION_ID + ".provider", file);
希望这对您有用!
答案 2 :(得分:2)
如果构建的后缀不同,那么进行这样的更改是有意义的。
FileProvider.getUriForFile(mContext.get(), BuildConfig.APPLICATION_ID + ".fileprovider", file)
答案 3 :(得分:1)
res folder
这是我的提供程序声明,$ {application}的值为“ com.limxtop.research”,请确保授权机构的名称与以下代码的名称相同。
07-06 20:57:00.140 17098-17098/in.somepackage.debug E/AndroidRuntime: FATAL EXCEPTION: main
Process: in.somepackage.debug, PID: 17098
java.lang.NoClassDefFoundError: in.somepakage._offline._di.Mapper
at in.somepakage._di.DaggerApp_Component.getMapper(DaggerApp_Component.java:1145)
at in.somepakage._di.DaggerApp_Component.server(DaggerApp_Component.java:1931)
at in.somepakage._di.DaggerApp_Component.getSyncDirtyTransaction(DaggerApp_Component.java:1166)
at in.somepakage._di.DaggerApp_Component.getSyncDirtyTransactions(DaggerApp_Component.java:1170)
at in.somepakage._di.DaggerApp_Component.getSignout(DaggerApp_Component.java:1188)
at in.somepakage._di.DaggerApp_Component.getNotificationWorker(DaggerApp_Component.java:1228)
at in.somepakage._di.DaggerApp_Component.injectApp(DaggerApp_Component.java:2132)
at in.somepakage._di.DaggerApp_Component.inject(DaggerApp_Component.java:1946)
at in.somepakage.App.setupDependencyInjection(App.java:479)
at in.somepakage.App.onCreate(App.java:180)
因此,您的代码发布可能不完整,您应该在某些地方传递“ my.package.name.fileprovider”作为参数。
答案 4 :(得分:1)
mImageFromCamera = FileProvider.getUriForFile(this,BuildConfig.APPLICATION_ID +“。 fileprovider ”,mImageFile); android:authorities =“ $ {applicationId}。文件提供商”
xml和代码中的权限必须相同
答案 5 :(得分:1)
{applicationId}
== com.companyName.application
附加“ .provider”
这将是== com.example.test.provider
在xml authorities:com.example.test.provider
活动 Uri mPath = FileProvider.getUriForFile(this, "com.example.example.provider", imageFile);
答案 6 :(得分:0)
将权限更改为唯一名称以解决类似问题
android:authorities="${applicationId}.myUniquefileprovider"
也在Java代码中
答案 7 :(得分:0)
我刚刚从android:authorities =“ $ {applicationId} .provider”中删除了“ $”,现在它就像一个符咒。
答案 8 :(得分:0)
这里的问题是,您为清单中的授权使用类名称 .provider 并在Java代码中使用 .fileprovider 类名。
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths_app" />
</provider>
Couldn't find meta-data for provider with authority my.package.name.fileprovider
只需将fileprovider重命名为 provider
答案 9 :(得分:0)
就我而言;
清单文件:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths" />
</provider>
代码:
import androidx.multidex.BuildConfig // NOT DO THIS!!!
val uri = FileProvider.getUriForFile(this,
BuildConfig.APPLICATION_ID+ ".provider", _tempFile)
例外:
java.lang.IllegalArgumentException: Couldn't find meta-data for provider with authority androidx.multidex.provider
请勿使用androidx.multidex.BuildConfig,因为该值不是我们应用程序的值:
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package androidx.multidex;
public final class BuildConfig {
public static final boolean DEBUG = false;
public static final String APPLICATION_ID = "androidx.multidex";
public static final String BUILD_TYPE = "release";
public static final String FLAVOR = "";
public static final int VERSION_CODE = -1;
public static final String VERSION_NAME = "";
public BuildConfig() {
}
}
答案 10 :(得分:0)
首先使用
File(getExternalFilesDir(null),context?.getString(R.string.app_name));
代替
Environment.getExternalStorageDirectory(), context?.getString(R.string.app_name) // this is deprecated in API29
然后使用这个
Uri contentUri = getUriForFile(getContext(), "com.mydomain.fileprovider", newFile);
答案 11 :(得分:0)
您可以替换您的 BuildConfig
导入类文件名:
import androidx.multidex.BuildConfig;
与:
import com.yourAppName.BuildConfig;
答案 12 :(得分:-2)
它的文件提供者而不是提供者
android:authorities="${applicationId}.fileprovider"