I need to build an application which enables me to edit the build.prop by using my own application. This is to optimize some of the values in the build.prop. Of course, I can edit this file using a file manager with root access. But my problem is, I need to edit this using an application that I create, and make it easy for the novice users to edit the file with recommended settings by my application. Please help me figure out this. Thanks for you time!
Something like this should work. Code hasn't been tested and might need some changes
Requesting Superuser Access
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes("mount -o remount rw /system/\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
File file=new File("/system/build.prop");
Read file
FileInputStream fis = openFileInput("/system/build.prop");
String content = "";
byte[] input = new byte[fis.available()];
while (fis.read(input) != -1) {}
content += new String(input);
EDIT String content here.
Write file
DataOutputStream outstream= new DataOutputStream(new FileOutputStream(file,false));
String body = content;
outstream.write(body.getBytes());
outstream.close();
Remember to backup your build.prop in-case something goes wrong during editing.
Related
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 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.
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
The problem is this:
I make an internet connection to some url and receive an HttpResponse with an app_example.apk.
Then I want to create a file (an .apk)
in the sdcard with this data so that this downloaded application
can be installed later.
How can I convert the HttpResponse to an .apk file?
Let's clear some details:
I have to get this apk file through an internet connection to my server
I don't want to install this applications I receive on the sdcard
All of this has to be done in my code, I cannot use android market
I am currently writing to that file.
What I'm doing is converting the HttpResponse to a byte[ ],
then that byte[ ] is written to a file (an .apk) using an ObjectOutputStream.
Like this:
// byte[] appByteArray - already has the internet response converted in bytes
try {
file = new File(Environment.getExternalStorageDirectory()+"/"+appName+".apk");
file.createNewFile();
FileOutputStream stream = null;
stream = new FileOutputStream(file, false);
ObjectOutputStream objectOut =
new ObjectOutputStream(new BufferedOutputStream(stream));
objectOut.writeObject(appByteArray);
objectOut.close();
} catch (Exception e) {
e.printStackTrace();
}
In the end, the file is created
and has the received content.
When I try to install it,
through a VIEW intent (using the default installer)
I get a parse error saying that it could not find the AndroidManifest.xml.
I think that in some step along the way, the received data is being corrupted.
Do you have another method to solve this?
Many thanks
Don't use an ObjectOutputStream, byte array is serialized as Object, not written as raw data.
Are you sure that you have SD card write permission? android.permission.WRITE_EXTERNAL_STORAGE
Don't write into SD card root directory. Number of files in root dir can be limited. Instead create you app subdirectory on SD CARD.
This code works for me:
try {
String filePath = Environment.getExternalStorageDirectory()
+ "/myappdir/" + appName + ".apk";
File file = new File(filePath);
file.getParentFile().mkdirs();
file.createNewFile();
BufferedOutputStream objectOut = new BufferedOutputStream(
new FileOutputStream(file));
objectOut.write(appByteArray);
objectOut.close();
} catch (Exception e) {
e.printStackTrace();
}
This may not be the core problem, but I don't think you want to wrap stream in an ObjectOutputStream, since that is used for object serialization. It could be that it is adding extra data to the file so it can be deserialized with ObjectInputStream.
I would try pulling the apk off of the emulator (or device) and check it's MD5 versus the file on the server to make sure that the bits are being written out correctly.
Take a look at Pavel P's answer.
Also, I would note that your idea of installing the APK using the VIEW intent action does work, as I have tested this technique in the past.
However, unless the user has explicitly gone into Settings → Applications and selected "Allow non-Market applications", your installation will fail and the user will just see a screen telling them that for security reasons the installation has been blocked.
Basically you really need to rely on having fairly tech-savvy users who are willing to overlook a scary security warning and go and disable that setting.