I had to port my game from Cocos2dX to Unity for various reasons.
I have now ported the project successfully but to launch it I have to make a mechanism to get old user data and store it in new structure, like number of levels locked, high score of users etc.
While searching I came to know that COCOS2dX stored data on user's device on following path
system/data/data/mygamepackage/shared_prefs/Cocos2dxPrefsFile.xml
Is there anyway to get data from above mentioned path?
The above path should be accessible by the app it self.
I am not able to read that file I get following error.
ENOENT No Such file or Directory
The file is there I can see that on rooted device via Root Browser but I get the error when I run the app on same device.
All I need is to access the file programmatically and later I will parse it and will store it via Unity for future use.
Looking forward for a positive and quick response.
I dont know why I am unable to get the file through Unity Engine, I had to write custom plugin is Java and use that in Unity Project and that some how has worked.
Following is the code which return string after reading the file on both rooted and non rooted devices
public static String getCocos2DPrefsFile(String mPackageName)
{
//Get the text file
File file = new File("/data/data/"+mPackageName+"/shared_prefs/","Cocos2dxPrefsFile.xml");
//Read text from file
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
Log.d("Error", e.toString());
}
return text.toString();
}
Related
I am trying to create a local web server using NanoHTTPD on Android. I know how to use it for a single file doing something like:
#Override
public Response serve(IHTTPSession session) {
String answer = "";
try {
FileReader index = new FileReader(fileLocation);
BufferedReader reader = new BufferedReader(index);
String line = "";
while ((line = reader.readLine()) != null) {
answer += line;
}
reader.close();
} catch(IOException ioe) {
Log.w("Httpd", ioe.toString());
}
return newFixedLengthResponse(answer);
}
and setting it to port 8080 so I can browse to http://localhost:8080 and view the file. However, I would like to use NanoHTTPD to put an entire directory on the server and not just a single file, in order to run an HTML5 game. Kinda similarly to how I can browse to a folder in Command Prompt on Windows and use
python -m http.server 4444
in order to browse through the folder as if it were a website at http://localhost:4444. I'm not sure how to go about it using NanoHTTPD. Can someone help me figure out where to start, or is there another library out there that's much easier to use?
Thanks in advance. Any help would be greatly appreciated.
You are on the right path.
Quick answer look here.
What you shod do is generate HTML files of directory structure to the user.
Meaning when the user is first contacting you read the local directory that you determine as root directory and create an HTML file as a response with the file names as links.
If the user then clicks on a single file serve it like you already do.
I found an answer demonstrating how to generate HTML files dynamically that you can use here.
This question already has answers here:
How do I read the file content from the Internal storage - Android App
(6 answers)
Closed 6 years ago.
I want to open a file in android using the following code:
FileInputStream fis = openFileInput("examplelist.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
but where to put the file examplelist.txt? I found some contradicting sources, like this and this? How to do it correctly?
Update:
I want to be able to replace this file at anytime after the app has been installed. So this file is NOT part of the source code or anything. It contains dynamical data...
I would have used a simple 'File Selector', so the user can navigate to the file anywhere on the android phone, but this looks terribly complicated and cumbersome. So, for now, I just want to open a file I can put somewhere on the phone in the most possible simple manner...
In order to be able to access a file, you should place it in /app/src/main/assets/examplelist.txt. You can access this directory in the Project explorer in Android Studio.
To open the file from java code, you can create an InputStream object by calling getAssets().open("examplelist.txt")
One possible way to solve this problem is the following:
You put the file in the kind-of-root folder, which might have the following path: /storage/emulated/0 (this is the location you can see when you connect the device e.g. to Linux and open it in the file explorer).
You can open the content of the file using the following code:
String path = Environment.getExternalStorageDirectory().toString();
Log.d("Files", "Path: " + path);
String joinedPath = new File(path, "examplelist.txt").toString();
try {
FileInputStream fis =new FileInputStream(new File(joinedPath));
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader bufferedReader = new BufferedReader(isr);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
Log.d("Text", sb.toString());
} catch (IOException e) {
Toast.makeText(getBaseContext(), "File does not exist", Toast.LENGTH_LONG).show();
}
VERY IMPORTANT, and which I forget EVERY single time: You have to set permission to the app to access the storage. You have to put the permission tag in the AndroidMainfest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
...
but where to put the file examplelist.txt?
Your app would create the file itself. openFileInput() is part of internal storage, to which ordinary users have no access.
I would have used a simple 'File Selector', so the user can navigate to the file anywhere on the android phone, but this looks terribly complicated and cumbersome.
Step #1: Call startActivityForResult() with an ACTION_OPEN_DOCUMENT or ACTION_GET_CONTENT Intent (the former is for Android 4.4+)
Step #2: In onActivityResult(), if you get RESULT_OK as a result, get the Uri to the user's chosen content via getData() on the Intent passed into onActivityResult()
Step #3: Call openInputStream() on a ContentResolver to get an InputStream to use for reading the content
It's about 10-15 lines of code over what you have in your question, plus the code that your question needs but does not show (e.g., background thread for I/O).
Or, use a file chooser library.
I just want to open a file I can put somewhere on the phone in the most possible simple manner...
Use external storage, such as getExternalFilesDir().
I am working n in the meanwhile learning to code in android
I came across this mediastore which returns all the details about all the mp3 files stored on the card(Internal & External).
But this method is very very slow
I guess that's what has been implemented in the default music application
No wonder it sometimes fails to find out all the files...
I was thinking of implementing a faster search algorithm for this purpose, But am confused with the initial requirements of these algorithms
1: I thought of implementing the Binary search method (Divide n Conquer) to find files, but then the algorithm requires information about the number of files to be scanned.How do I get that?
2: I thought of implementing separate threads for each divided cluster.But then will it really work?
Plz help me in this!
the last question : How in the world does poweramp find out all the files so quickly,
My android has about 200 songs on the card, but this app only takes some seconds to get them all!!
Really puzzled!!
Try this code:
go to Download and DCIM folder there you can use this command to get all files and if you want to filter files then here is the link
Process process = null;
try {
process = Runtime.getRuntime().exec("ls -l");
} catch (IOException ez) {
ez.printStackTrace();
}
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
StringBuilder total = new StringBuilder();
String line;
try {
while ((line = in.readLine()) != null) {
System.out.println("line: "+line.toString());
total.append(line);
}
} catch (IOException ex) {
ex.printStackTrace();
}
System.out.println("Command Output: "+total.toString());
I have 4 txt files in my desktop.
I create an android app where I will read these files and print these in an EditText, but
these files are drug drop in ddms -> myapp -> data -> files.
So with emulator my app play correctly, but in my mobile phone, don't read these files and close the app.
This is my code:
public String ReadFileTrack1()
{
FileInputStream fIn = null;
InputStreamReader isr = null;
char[] inputBuffer = new char[900];
String data = null;
try
{
fIn = openFileInput(tracks[0]+".txt");
isr = new InputStreamReader(fIn);
isr.read(inputBuffer);
data = new String(inputBuffer);
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
isr.close();
fIn.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
return data;
}
The code is correct, but don't play in my mobile phone.
What I can do for this problem?
How can i load these 4 txt files in my mobile phone so that my app play correctly?
You cannot write to an app's private storage directory on a secured device, unless the application first makes a subdirectory and grants others write access to it (which is generally a bad idea).
Put the files on the external storage (sdcard) instead, or package them as assets in the apk when you build it.
(There is an exception: if the application is built debuggable, then one can use the adb shell's run-as tool to copy something into the private directory as the appliction user, but that's obscure and not something you'd want to use as it does not apply to finalized apps. Generally that capability is used in the other direction, to examine what an app under development has done to its files or database when it is not working correctly)
I make android application that read textfile.txt from resource/raw. And it's work fine. Here is the code:
BufferedReader bufferreader = new BufferedReader(new
InputStreamReader(getResources().openRawResource(R.raw.textfile)));
String bufferLine;
String tempText = "";
try{
while ((bufferLine = bufferreader.readLine()) != null){
tempText = tempText + bufferLine;
}
} catch (IOException e){
}
Now, I need also to access this raw resource textfile by modify(write) or delete the content. Is there anyway to do it on android ?
Use the external storage to store the file after you have modified it.
How you use the external storage depends a bit on what version you are targeting, but it's explained in the documentation that I linked to.
Files that you place in the external storage associated with your application will be deleted if your application is uninstalled and your target version is API level 8 or above.
You can't write resources. Copy the file in the application internal memory or in the external storage (sd card or else) and then you can modify it.