I have this code:
try {
URL url = new URL (strURL);
input = url.openStream();
byte[] buffer = new byte[1500];
OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");
and I get this error:
do not hardcode /sd card/ use environment.getexternalstoragedirectory().getpath() instead
in
OutputStream output = new FileOutputStream ("/sdcard/"+pos+".png");
I have read about that:Android 4.2 - Environment.getExternalStorageDirectory().getPath() behaviour
So the question is what will be final code when I replace it?
sd folder name different in Some phones
In samsung phones, it is named external_sd , and your code will fail.
control+shift+o --> to add imports in eclipse,see this link
"/sdcard/" is replace with "Environment.getExternalStorageDirectory().getPath()" in your code
The problem was that you call a file called Environment.java itself, so Eclipse didn't give me the choice to import Environment change that one.
Your revised code should look like
OutputStream output = new FileOutputStream(Environment.getExternalStorageDirectory().getPath()+pos+".png");
You should be careful as not all devices have a /sdcard/ path.
Devices across the years have changed and this may not always contain a link to the proper external storage directory.
Related
I'm testing my app (with android, core and desktop modules) on desktop, everything works fine.
Now that I try to run on Android, it gives me FileNotFound. I linked android asset to desktopLauncher before running on desktop, but Am I meant to do something similar for android?
File file = Gdx.files.internal("liver_initial_joints.json").file();
FileInputStream fis = new FileInputStream(file);
byte[] data = new byte[(int) file.length()];
fis.read(data);
fis.close();
str = new String(data, "UTF-8");
I have my file at .../android/assets/liver_initial_joints.json
Android does not provide direct java.io.File access to the internal app directory. If you look at the javadocs for libgdx's FileHandle.file(), you'll see that it says it's not usable for anything but Absolute and External types of files.
If you just need to read a string or byte array, you can use fileHandle.readString() or fileHandle.readBytes(). If you need an input stream, use fileHandle.read();.
For example:
byte[] data = Gdx.files.internal("liver_initial_joints.json").read();
str = new String(data, "UTF-8");
I find a really easy way to do this is with:
InputStream is = context.getAssets().open("liver_initial_joints.json");
Looking for a way to prevent users from opening a text file that i transfer from android to PC
Through DropBox.
is convert the text file to bin file Will do the job ?
can i get any sample code in java (for android) that convert text file to bin file ?
i try this but dont work:
Reading the file:
File queryImg = new File(ImagePath);
int imageLen = (int)queryImg.length();
byte [] imgData = new byte[imageLen];
FileInputStream fis = new FileInputStream(queryImg);
fis.read(imgData);
Writing the file:
FileOutputStream f = new FileOutputStream(new File("/MyPath/xx.bin"));
f.write(imgData);
f.flush();
f.close();
If I correctly understand the question, I'd suggest either changing file extension from .txt to otherwise nonexistent extension of your choice (.gldsft or something like that), encrypting, or not going with text file at all.
I need to download some pdf files into data/data/com.**.* folder.
Those files are application specific and only application should read and display it that's the reason storing on data/data/com.**.* folder.
Please let me know how to download into that folder and open/read it in the application.
I know how to download it into SD card, but I do not have idea to downloading to application specific folder.
Please let me know some code examples to do this and also I need to know the capacity/size of the data/data/com.**.* folder.
As long as you want write your own applications Data folder, you can create a FileOutputStream like this FileOutputStream out = new FileOutputStream("/data/data/com.**.*/somefile"); than use that output stream to save file. Using the same way you can create a FileInputStream and read the file after.
You will get Permission Denied if you try to access another application's data folder.
I am not sure for capacity but you can calculate the size of the data folder using this
File dataFolder = new File("/data/data/com.**.*/");
long size = folderSize(dataFolder);
...
public static long folderSize(File directory) {
long length = 0;
for (File file : directory.listFiles()) {
if (file.isFile())
length += file.length();
else
lengthlong += folderSize(file);
}
return length;
}
Hi here i am attaching the link of a tutorial explained.
http://www.mysamplecode.com/2012/06/android-internal-external-storage.html
and there are many discussions going on internet that you should root your phone in order to access the data from data/data folder and I am also attaching some links about the discussion, I hope these are also some of the links that are related to your question
where do i find app data in android
How to access data/data folder in Android device?
and as well as some links that makes out the things without rooting your phone i mean
You can get access to /data/data/com*.* without rooting the device
http://denniskubes.com/2012/09/25/read-android-data-folder-without-rooting/
To Write file
FileOutputStream out = new FileOutputStream("/data/data/your_package_name/file_name.xyz");
To Read file
FileInputStream fIn = new FileInputStream(new File("/data/data/your_package_name/file_name.xyz"));
Now you have your input stream , you can convert it in your file according to the file type .
I am giving you example if your file is contain String data the we can do something like below ,
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String mDataRow = "";
String mBuffer = "";
while ((mDataRow = myReader.readLine()) != null) {
mBuffer += mDataRow + "\n";
}
Remember to add write file permission to AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I'm writing an airplane simulator program and want the user to be able create model files seperately using XML. Then, in the app they can select which model (XML file) to load. I have the XML files in a subdirectory on my SD card and can read it with my BLU phone but not my Motorola Photon II. With the Motorola I get a file not found when initializing the input stream. I have the following code...
In the Manifest I have set read external storage permission...
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Here's the code to open the XML file...
File file = new File(Environment.getExternalStorageDirectory().getPath() + "/SimFiles/Vehicles/" + "myModel.xml");
InputStream in = new BufferedInputStream(new FileInputStream(file));
It's pretty straight forward. So, why on one and not the other? Is it a permissions thing- one more strict than the other? Thanks.
Try using this...
File file = new File(Environment.getExternalStorageDirectory() + "/SimFiles/Vehicles/" + "myModel.xml");
InputStream in = new BufferedInputStream(new FileInputStream(file));
I want to create an inputstream for a file that is stored in the Downloads folder of my phone.
Earlier I had the required file as an asset and I read it with this code:
InputStream fin;
fin = getAssets().open(FILENAME);
However now I want to be able to open a file stored in my downloads folder /mnt/sdcard/Download as an InputStream NOT as a FileInputStream Object. I am using a library which requires an InputStream only as an argument so I can not use the FileInputStream
methods because I get a fatal exception if I do and the App stops.
How do I go about this? I tried to find some helper function from the documentation, but could not.
Edit: I also tried editing the code to this-
String path = "/mnt/sdcard/Download"+FILENAME;
fin = new BufferedInputStream(new FileInputStream(path));
However still when I try to read the file, the App crashes.
I had the same issue as yours :
AssetManager am=getAssets();
InputStream is=am.open("file1.xls");
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);
Here I used to access file1.xls from the assets folder.
I fixed it by using :
File filepath = newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+"file1.xls"); // fist part gets the directory of download folder and last part is your file name
InputStream is = new FileInputStream(filepath);
Workbook wb = Workbook.getWorkbook(is);
Don't forget to get the storage permission first to access the storage.