Govind Narayan
Govind Narayan

Reputation: 101

Solution for updating Assets folder

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

    }

Upvotes: 2

Views: 6472

Answers (1)

BitBank
BitBank

Reputation: 8715

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.

Upvotes: 3

Related Questions