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.
Related
I am making an android webview application,there are a lot of files such as js/css/images must be downloaded from CDN to the app, because the network is not stable in our region and the size of the cached files is much bigger than the app itself, is there some way to build the apk file with the cache files stored automatically when the app runs at first few times.
Put your files and folders to assets. you'll find it in your project directory. When your application runs copy all assets contents to your SD card. then run your app :)
If you need any help about how to copy assets content to SD card let me know.
Copy Assets contents to SD card
Bellow code will copy all the contents of specified folder of your assets to your specified location of your SD card
CopyAssetContents.java
public class CopyAssetContents {
public static boolean copyAssetFolder(AssetManager assetManager,String fromAssetPath, String toPath) {
try {
String[] files = assetManager.list(fromAssetPath);
new File(toPath).mkdirs();
boolean res = true;
for (String file : files)
if (file.contains("."))
res &= copyAsset(assetManager,
fromAssetPath + "/" + file,
toPath + "/" + file);
else
res &= copyAssetFolder(assetManager,
fromAssetPath + "/" + file,
toPath + "/" + file);
return res;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
private static boolean copyAsset(AssetManager assetManager,
String fromAssetPath, String toPath) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(fromAssetPath);
new File(toPath).createNewFile();
out = new FileOutputStream(toPath);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
return true;
} catch(Exception e) {
e.printStackTrace();
return false;
}
}
private static 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);
}
}
}
For example you have all your contents in a folder named "CONTENTS" inside of your assets.and want to copy all of its content to root of your SD card.
call bellow method.
CopyAssetContents.copyAssetFolder(getAssets(), "CONTENTS", Environment.getExternalStorageDirectory().getAbsolutePath());
I've been looking at this site for the past 3 or so hours. How to copy files from 'assets' folder to sdcard?
This is the best I could come up with because I'm only trying to copy one file at a time.
InputStream in = null;
OutputStream out = null;
public void copyAssets() {
try {
in = getAssets().open("aabbccdd.mp3");
File outFile = new File(root.getAbsolutePath() + "/testf0lder");
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: ", e);
}
}
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);
}
}
I've figured out how to create a file and save a text file. http://eagle.phys.utk.edu/guidry/android/writeSD.html
I would rather save an mp3 file to the sdcard rather than a text file.
When I use this code I provided, I get a text document that same size as the aabbccdd.mp3 file. It does not create a folder and save an .mp3 file. It saves a text document in the root folder. When you open it, I see a whole bunch of chinese letters, but at the top in English I can see the words WireTap. WireTap Pro was the program I used to record the sound so I know the .mp3 is passing through. It's just not creating a folder and then saving a file like the above .edu example.
What should I do?
I think you should do something like that -[Note: this i used for some other formats not mp3 but its works on my app for multiple format so i hope it will work for u too.]
InputStream in = this.getAssets().open("tmp.mp3"); //give path as per ur app
byte[] data = getByteData(in);
Make sure u have the folder already exists on path, if folder is not there it will not save content correctly.
byteArrayToFile(data , "testfolder/tmp.mp3"); //as per ur sdcard path, modify it.
Now the methods ::
1) getByteData from inputstream -
private byte[] getByteData(InputStream is)
{
byte[] buffer= new byte[1024]; /* or some other number */
int numRead;
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
try{
while((numRead = is.read(buffer)) > 0) {
bytes.write(buffer, 0, numRead);
}
return bytes.toByteArray();
}
catch(Exception e)
{ e.printStackTrace(); }
return new byte[0];
}
2) byteArrayToFile
public void byteArrayToFile(byte[] byteArray, String outFilePath){
FileOutputStream fos;
try {
fos = new FileOutputStream(outFilePath);
fos.write(byteArray);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
How can I get all list of .json config files, which are located NOT on SD card, but in my package? Bellow on the image showed the files in the folder which I want to access. The idea is the following: when application starts I want to get list of config files, the path to them and display that to testers to choose to what server connect to. How can I do that? I was looking for PackageManager and AssetManager, but resultless.
When I put folder configs to folder assets, this code game me a list of available configs, but how can I get full path to them to read them?
AssetManager am = getAssets();
String s = getPackageName();
Resources tmp = pm.getResourcesForApplication(s);
String [] arr = am.list("configs2");
To do a similar thing I've used the following command:
InputStream is = context.getResources().openRawResource(R.raw.raw_data_file);
Where the file raw_data_file was in the path res/raw/raw_data_file.txt. You can then read the file using the InputStream as normal. I'm sure you'd be able to do a similar thing with your file where it is, but as a rule I'd normally put any resources in the res(ources) folder
I finally solved my problem. This is the structure of my project:
To get list of files from application package, you need to put all your files you want to get into assets folder. Here is the code:
private ArrayList getPackageConfigList()
{
AssetManager am = getAssets();
String [] arr = null;
try
{
arr = am.list(folder);
}
catch(IOException e) {e.printStackTrace();}
ArrayList<String> flist = new ArrayList<String>();
for (int i=0; i<arr.length; i++)
{
flist.add(PKG + arr[i]);
Log.d(tag, PKG+ arr[i]);
}
return flist;
}
To read concrete file from package:
private String loadConfigFromPackage(String fileName)
{
AssetManager am = getAssets();
InputStream in = null;
String result = null;
try
{
//open file, read to buffer, convert to string
in = am.open(folder + "/" + fileName);
int size = in.available();
byte[] buffer = new byte[size];
in.read(buffer);
in.close();
result = new String(buffer);
}
catch(IOException e)
{
e.printStackTrace();
}
finally
{
try
{
in.close();
}
catch(Exception ex){}
}
return result;
}
I need to find a way how to create files from specific folder in Internal Storage of my device to a specific folder in External Storage.
Example :
I have 50 image files in data/data/app_package/files/documents/server/userId/storage/ in Internal Storage.
I want to copy all of the files in that directory to /sdcard/Documents/Server/UserId/Storage/
And the idea is that in some cases maybe I'll have to move files like 50MB and maybe more. Any suggestions how can I achieve this?
try this code
private void copyToFolder(String path) throws IOException {
File selectedImage = new File(path);
if (selectedImage.exists()) {
String wall = selectedImage.getName();
in = getContentResolver().openInputStream(selectedImageUri);
out = new FileOutputStream("/sdcard/wallpapers/" + wall);
copyFile( in , out); in .close(); in = null;
out.flush();
out.close();
out = null;
} else {
System.out.println("Does not exist");
}
}
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);
}
}
I am trying to create a folder and several subdirectory within it on the SD Card... I then want to transfer files that I have stored in /res/raw to that folder... I addition, I want this to only happen once, the first time the program is ever run. I realize that this is ridiculously open-ended, and that I am asking a lot... but any help would be greatly appreciated.
This will copy all files in the "clipart" subfolder of the .apk assets folder to the "clipart" subfolder of your app's folder on the SD card:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
String basepath = extStorageDirectory + "/name of your app folder on the SD card";
//...
// in onCreate
File clipartdir = new File(basepath + "/clipart/");
if (!clipartdir.exists()) {
clipartdir.mkdirs();
copyClipart();
}
private void copyClipart() {
AssetManager assetManager = getResources().getAssets();
String[] files = null;
try {
files = assetManager.list("clipart");
} catch (Exception e) {
Log.e("read clipart ERROR", e.toString());
e.printStackTrace();
}
for(int i=0; i<files.length; i++) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open("clipart/" + files[i]);
out = new FileOutputStream(basepath + "/clipart/" + files[i]);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch(Exception e) {
Log.e("copy clipart ERROR", e.toString());
e.printStackTrace();
}
}
}
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);
}
}
I experienced a similar problem when using mkdirs(), however because running the command:
mkdir one/two
fails on Linux, then the method http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdirs() subsequently fails too. I guess this means there is no way to use mkdirs on Android? My (probably rather hacky) work-around was to create each necessary directory separately:
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
new File(extStorageDirectory + "/one/").mkdirs();
new File(extStorageDirectory + "/one/two/).mkdirs();