gpt4 book ai didi

android - 更新Assets文件夹的解决方法

转载 作者:搜寻专家 更新时间:2023-10-30 22:12:47 25 4
gpt4 key购买 nike

我正在制作一个测验应用程序,我将图像存储在 Assets 文件夹中,比如 Assets /图片。我使用 AssetsManager 从 Assets 文件夹中检索图像。但是在我向 Assets 文件夹添加更多图像后,我发现该应用程序未获取更新 Assets 文件夹中的图像,除非我卸载以前的应用程序。我还了解到,除非我卸载旧应用程序,否则无法更新 Assets 文件夹。我说得对吗?我想知道是否可以将 Assets 文件夹图像添加到数据库并使用数据库检索图像到应用程序?另外,当我发布更新版本的应用程序时,我的新图像文件是否会显示?如果可以,如何?

这是我用来从 assets 文件夹中拉出的东西

String[] getImagesFromAssets() {
AssetManager assetManager = getAssets();
String[] img_files = null;
try {
// img_files = getAssets().list("pictures");
img_files = assetManager.list("pictures");

} catch (IOException ex) {
Logger.getLogger(GameActivity.class
.getName()).log(Level.SEVERE, null, ex);
}
return img_files;


}

void loadImage(String name) {
AssetManager assetManager = getAssets();
System.out.println("File name => "+name);
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("pictures/"+name); // if files resides inside the "Files" directory itself
out = new FileOutputStream(Environment.getExternalStorageDirectory() +"/" +"pictures/"+ name);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
Drawable d=Drawable.createFromPath(Environment.getExternalStorageDirectory() +"/" +"pictures/" + name);
image.setImageDrawable(d);
//Drawable d = Drawable.createFromStream(in, null);
//image.setImageDrawable(d);
} catch(Exception e) {
Log.e("tag", e.getMessage());
}


}

private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}

}

最佳答案

Assets 文件夹是应用程序只读的构建时资源。一旦在 APK 中指定了文件,就无法在运行时更改它们。您将需要使用本地存储来处理您的应用程序创建的新文件。

关于android - 更新Assets文件夹的解决方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21372722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com