You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Toasty赋值?需将字符串存入XML而非硬编码

解决方法

1. 添加XML字符串资源

在项目的res/values/strings.xml文件中,添加对应的字符串资源:

<string name="toast_media_add_success">This message should be placed in the xml string!!!</string>

2. 修改Toasty代码调用

将原代码中硬编码的字符串替换为从资源中获取的字符串,利用上下文c调用getString()方法:

// 替换原Toasty调用行
Toasty.success(c, c.getString(R.string.toast_media_add_success), Toast.LENGTH_LONG).show();

完整修改后的代码片段:

//------------insert into media list----
File newfilee = new File(destination.getPath());
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, destination.getPath());
values.put(MediaStore.Images.Media.DATE_TAKEN, newfilee.lastModified());
scanPhoto(newfilee.getPath());
// 修改后的Toasty调用
Toasty.success(c, c.getString(R.string.toast_media_add_success), Toast.LENGTH_LONG).show();
Uri uri1;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    uri1 = FileProvider.getUriForFile(c.getApplicationContext(), 
 c.getPackageName() + ".provider", newfilee);
} else {
    uri1 = Uri.fromFile(newfilee);
}
c.getContentResolver().notifyChange(uri1, null);
Utils.IsUpdate=true;
ViewImageActivity.list.add(0,NewImagePath);

内容的提问来源于stack exchange,提问作者EugeneSermyagin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 04:32:20