My project is all about media player. I want to store manually hundreds of mp3 files in my project but the problem is I don't know where I can put the large file which is the file size is 300MB into my project. I already search many times on google but the answer is always putting the file into ExternalDirectory by using this code.
File file = new File(getExternalFilesDir(null)+"/");
The problem of this code is you can only store a file to this directory once you downloaded the file. Since my project is an offline app, I don't need an internet connection to it
Here is my first attempt.
I put my one mp3 file to asset folder which is the size is 5mb and make a copy to another directory
File file = new File(getExternalFilesDir(null)+"/newFile.mp3"); please take a look below.
private void copyFile() {
InputStream inputStream;
try {
inputStream = getAssets().open("raw/original.mp3");
File file = new File(getExternalFilesDir(null)+"/newFile.mp3");
OutputStream outputStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
Log.d(TAG, "copyFile: copied ");
} catch (IOException e) {
e.printStackTrace();
}
}
the code above works fine. I can see that the file was successfully copied but the problem is during my research they said that asset folder or res/raw folder has a storage limit it means that I can only store a file in asset folder with a file size less than 5MB (I am not sure).
My question is what is the best way to manually store mp3 files (300MB) to my project(without downloading the files)?
Thanks and advance.
Related
I have a number of pdf files (but I might extend the functionality to other document types as well) that I want to show in my app. Static images go in drawable folder, static text goes in the strings file. But where do I put pdf files?
I know I can host it on a server and have a one-time-download kind of thing, but for my app's use case that's impossible. The app is being designed for a very specific use case in mind and I absolutely need to bundle the pdf files along with the app.
Create directory named assets in your app and put your pdf files in that directory. use this to read and display pdf files.
I think you can keep your PDF file (or any other file type) inside assets folder. So while downloading the APK form store, it will download those files too. Only problem is it will increase the APP size.
Check the below answer how you can access the PDF file from the Assets using assetManager
https://stackoverflow.com/a/17085759/7023751
I'm answering my own question because other answers didn't fully work for me.
Like the other answers, I read the file from assets folder and created a file in internal storage. I used muPDF which works perfectly with file URI.
items is an ArrayList of file names.
try
{
AssetManager assetManager = getAssets();
InputStream in = assetManager.open(items.get(position));
byte[] buffer = new byte[in.available()];
in.read(buffer);
File targetFile = new File(getFilesDir(), items.get(position));
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
in.close();
outStream.flush();
outStream.close();
//Change below intent statements as per your code
Intent intent = new Intent(this, DocumentActivity.class);
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.fromFile(targetFile));
startActivity(intent);
}
catch (Exception e)
{
e.printStackTrace();
}
I am working on an android application where I want users to upload a file and I want to read the contents of uploaded file and display it . I have been reading files from SD card but I now I need the user to upload a file . I searched a lot but I didn't get any solution for it.
My code for reading the file from SD card
File file = new File(dir, "/tounzip/b.txt");
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
DataInputStream input = new DataInputStream(fileInputStream);
Its reading the file contents successfully but Is there any way to make the user upload the file and read the contents from it ? Any help would be great !! Thanks !!
Android file chooser
You require a file picker/file chooser.
Logic is you read the stream and save the stream in your app sandbox (app's memory i.e. /data/package..) then do whatever you want from there.
If file size is small then even a in-memory implementation will help.
I guess I'm a little confused as to how files are stored on an actual machine (or emulator even).
While programming, I can save my xml file in the assets folder manually, but how to write an app that will have to connect to the network and download the file,save it somewhere and then manipulate it ? where will it store said file ?
I want to create a new file, but I read on another post that the assets folder as such is not available once packaged; So where are they created and stored ? How can they be transferred. Its just, I'm new to this platform and the file system is a little confusing.
If you want to use XML that is updated, you should think of copying the file(s) from assets to device storage. You can take a look at How to copy files from 'assets' folder to sdcard? to know how this can be done.
Another alternative is to use the database where you can store the parsed data from the XML. So that you need not parse the file whenever you need to access the contents.
You have two options: call getFilesDir() from your activity to obtain a path to the internal data folder that can only be read/write from your app.
Or, you can write/read your xml file to external storage (SD Card). Use the method Environment.getExternalStorageDirectory() to get the root path of the external storage, then create your own folder as you see fit.
Note that if you write to external storage, every app in the phone will have access to it.
Even I faced this issue. Now I have a xml file which is has application properties.This is packaged in the assets folder.Once packaged we cannot edit a file in assets folder.
Now on app load I just copy this file to path returned by
context.getFilesDir().getAbsolutePath();
And the application edit it from the same place. You can see if the file is modified in the FileExplorer panel of DDMS view. The file is stored in the folder named same as your application package name for eg: com.abhi.maps
Alternatively you can also copy it to SD card.However it is risky because, sd card may bot be available all the time.
You can use the following code to copy file from assets folder:
private static void copyFile(String filename, Context context) {
AssetManager assetManager = context.getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
String newFileName = context.getFilesDir() + "/" + filename;
out = new FileOutputStream(newFileName);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
Hope it helps! :)
I want to be able to copy a file from within my apk to the sd-card and was just wondering if it was possible and how i would go about it as im struggling to find and infomation and the android chat rooms are all locked! Thanks in advance :)
Edit: I would like to push the file to the internal storage (prefrably /system/ but i can move it from the internal storage to there using terminal commands if needed)
Put your file inside assets folder on your project, e.g. file.txt.
Get input stream to read the file:
InputStream is = getAssets().open("file.txt");
Copy the contents:
FileOutputStream os = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "file.txt"))
byte[] buf = new byte[4096]
while (true) {
int len = is.read(buf);
if (len < 0) break;
os.write(buf, 0, len);
}
os.close();
is.close();
This is about how to push into internal storage http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
Im getting an input stream, which is an mp3 in my raw folder, and then I am trying to write it back to the raw folder (I know this sounds stupid but I want to do it), but I get an error - file not found, myFile.mp3. Any ideas where I am going wrong?
try
{
File f=new File("android.resource//com.apps/raw/myFile.mp3");
InputStream myRawResource = context.getActivity().getResources().openRawResource(mp3ID);
OutputStream out=new FileOutputStream(f);
byte buf[]=new byte[1024];
int len;
while((len=myRawResource.read(buf))>0)
out.write(buf,0,len);
out.close();
myRawResource.close();
}
catch (IOException e){
//error
}
You can't write the raw and assets folder in android.
Edit:
Android documentation
I copied the key phrase (Tip from the documentation):
If you want to save a static file in your application at compile time, save the file in your project res/raw/ directory. You can open it with openRawResource(), passing the R.raw. resource ID. This method returns an InputStream that you can use to read the file (but you cannot write to the original file).
This can't be done because resources, at runtime, are read-only, are stored in the APK, and would need to be compiled for inclusion in R at compile time.