解决方案更新Assets文件夹 [英] Solution for updating Assets folder

查看:152
本文介绍了解决方案更新Assets文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个测验应用程序,我已存储在资产文件夹的图像说资产/ pictures.I检索资产的图像文件夹使用AssetsManager.But后,我几个图像添加到文件夹的资产,我发现该应用程序不获取更新从资产文件夹中的图像,除非我卸载previous app.I还了解到,它是不可能的,除非我卸载旧app.Am我纠正更新资产文件夹?我想知道,如果它可以增加资产的文件夹的图像数据库,并利用数据库​​检索图像,应用程序吗?还有会当我释放我的应用程序的更新版本将显示我的新的图像文件?如果是的话怎么样?

这是我使用RAD从资产文件夹

 的String [] getImagesFromAssets(){
    AssetManager assetManager = getAssets();
    的String [] img_files = NULL;
    尝试{
       // img_files = getAssets()清单(图片);
        img_files = assetManager.list(图片);    }赶上(IOException异常前){
        Logger.getLogger(GameActivity.class
                。.getName())日志(Level.SEVERE,空,前);
    }
    返回img_files;
}无效的LoadImage(字符串名称){
    AssetManager assetManager = getAssets();
       的System.out.println(文件名=>中+名);
    在的InputStream = NULL;
    出的OutputStream = NULL;
    尝试{
         在= assetManager.open(图片/+姓名); //如果文件驻留在Files目录里面本身
      OUT =新的FileOutputStream(Environment.getExternalStorageDirectory()+/+图片/+姓名);
      copyFile(IN,OUT);
      附寄();
      在= NULL;
      了out.flush();
      out.close();
      出= NULL;
      可绘制D = Drawable.createFromPath(Environment.getExternalStorageDirectory()+/+图片/+姓名);
      image.setImageDrawable(四);
      //绘制对象D = Drawable.createFromStream(中,NULL);
    //image.setImageDrawable(d);
    }赶上(例外五){
        Log.e(标签,e.getMessage());
    }
}私人无效copyFile(在的InputStream,OutputStream的了)抛出IOException
字节[]缓冲区=新的字节[1024];
INT读;
而((读= in.read(缓冲))!= -1){
  out.write(缓冲,0,读);
}    }


解决方案

Assets文件夹是只读的应用程序构建时间资源。一旦文件被在APK指定,它们不能在运行时改变。您需要使用本地存储与新文件的工作,你的应用程序创建。

i am making a quiz app and i have stored images in assets folder say assets/pictures.I retrieve images from assets folder using AssetsManager.But after i add few more images to assets folder i found that the application do not fetch updated image from assets folder unless i uninstall the previous app.I also learned that its isn't possible to update assets folder unless i uninstall old app.Am i correct? I would like to know if its possible to added assets folder images to a database and use database to retrieve images to app?Also will my new image files will be shown when i release an updated version of my app?If so how?

This is what i use to rad from assets folder

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);
}

    }

解决方案

The Assets folder is a build-time resource that is read-only for the Application. Once the files are specified in the APK, they cannot be changed at run time. You will need to use local storage to work with new files that your application creates.

这篇关于解决方案更新Assets文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆