Delete Android Default Browser History - android

I'm trying to develop an android application that could erase default browser's search history without rooting, but I'm stuck. Here is my source code
File file = new File("data/data/com.android.browser/databases/browser.db");
try {
String content = "";
if(!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(content);
bw.close();
Toast.makeText(MainActivity.this, "History Deleted From Default Browser", Toast.LENGTH_LONG).show();
System.out.println("Done");
} catch (IOException e) {
e.printStackTrace();
}
As i know browser's history will be stored in "browser.db" file, i can able to clear history only if I change the permission of browser.db file in command prompt through adb shell like "chmod 777 data/data/com.android.browser/databases/browser.db"
But i need to do it every time, i want to do this inside my application source code, I also tried Runtime.exec() methods to execute adb shell, actually History Eraser app can erase the history of default browser without root permission, Can any one please help me out in solving this mystery. Thanks in Advance.

Add the following permissions to AndroidManifest.xml:
<uses-permission android:name="com.android.browser.permission.WRITE_HISTORY_BOOKMARKS"/>
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
Then when you want to clear the history use:
Browser.clearHistory(getContentResolver());

Related

I am using Android studio I want to write local text file via emulator

I want to write text file in local pc via emulator and i have try a lot of way but not yet successes so i need help for any one who will recognized what my problem. below is my codes.
This code work without an error but working less.
public void WriteText() {
EditText txt=(EditText)findViewById(R.id.txtwrite);
try {
FileOutputStream fos = openFileOutput("D:/File.txt", Context.MODE_PRIVATE);
fos.write(txt.getText().toString().getBytes());
fos.close();
Toast.makeText(this, "Saved", Toast.LENGTH_LONG);
} catch (Exception e) {
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG);
}
}
Definition of Emulator
The Android SDK includes a mobile device emulator — a virtual mobile device that runs on your computer. The emulator lets you develop and test Android applications without using a physical device.
It's a virtual mobile device.which means an emulator is much like a mobile device.you can only create files inside it not in computer.
Use the following method to create a file in android
Set permission in manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
public void createFile(String sFileName, String sBody){
try
{
File root = new File(Environment.getExternalStorageDirectory(), "Notes");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, sFileName);
FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
Toast.makeText(this, "Done", Toast.LENGTH_SHORT).show();
}
catch(IOException e)
{
e.printStackTrace();
}
}
For browsing the newly created file How to access files in Emulator SD Card using File Explorer?
If you want to send data from android to a computer system take a look at http://www.pixelstech.net/article/1368328614-Android-socket-programming-example
You cannot save files from emulator to PC.
Because, Emulator is like a seperate device so it is not possible to create files on PC.
Instead of that, you can do like this work around.
Create and save text file in your emulator storage
public void WriteText() {
EditText txt=(EditText)findViewById(R.id.txtwrite);
try {
BufferedWriter fos = new BufferedWriter(new FileWriter(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+"File.txt"));
fos.write(txt.getText().toString().trim());
fos.close();
Toast.makeText(this, "Saved", Toast.LENGTH_LONG);
} catch (Exception e) {
Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG);
}
}
Once you done with your file creation, you can easily pull the created file from emulator to your pc using adb pull command.
ADB command
adb pull /sdcard/File.txt D:/
Dont forget to add following uses-permission in your manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Note: Give some space for your emulator SDcard when you create your emulator.

Android file writing

Having a problem writing out to a file, this code is taken directly from the android developer page and then tweaked a bit by me. Is there something i am missing? Quite new to Android development so sorry if it's something blatantly obvious.
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FileOutputStream outputStream;
String data = "hello";
File fileDir = new File("data.txt");
if (!fileDir.exists())
fileDir.mkdirs();
try {
outputStream = openFileOutput("data.txt",Context.MODE_PRIVATE);
outputStream.write(data.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
Basically, your problem is that you are trying to do it twice, once in a way that won't work, and once in a way that will, but hides the result.
File fileDir = new File("data.txt");
if (!fileDir.exists())
fileDir.mkdirs();
This would create a Java File object connected to a hypothetical file called "data.txt" located in the current working directory, which for an Android app is the root directory of the device - a place you most definitely are not allowed to write to. However, this may not obviously cause any errors, as the root directory exists so mkdirs() will do nothing, and you only create a File object, you don't actually try to create a file on "disk". Effectively this code does nothing for you - get rid of it.
Next you try something basically workable:
try {
outputStream = openFileOutput("data.txt",Context.MODE_PRIVATE);
outputStream.write(data.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
openFileOutput() is a method of a Context (Activity or Service) which creates an output stream to write to an actual file located in the private internal storage area of your app. This is all fine and good, and normally a good choice for storing typical data. However, it is not a place that you will be able to examine when running a release app on a secured device, as neither ADB based tools nor Mass Storage or MTP access over USB have rights to it. So it's entirely possible that this code worked, but you had no way to discover that fact. If you are on an emulator, you can access this location with ADB or the DDMS browser, and if your apk is a debug one, you can use the run-as command line tool in the shell.
If you want to share the data, you might consider putting it on the External Storage instead.

Read private file (/data/data folder) in rooted device

I need to access a file contained in the private folder of another app. I have granted my app the root privilege and changed the permissions - although I think it's not necessary - but when I try to read from the file, I get "permission denied".
This is the code:
File file = new File("/data/data/other.app/shared_prefs/file.xml");
if(file.exists()) {
try {
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("chmod 777 " + file.getAbsolutePath());
InputStream in = new FileInputStream(file);
....
} catch (IOException e) {
e.printStackTrace();
}
}
Probably you wont be able to do this, because each app on android has a user with unique permissions.
See this: http://developer.android.com/guide/topics/security/permissions.html#userid
Any data stored by an application will be assigned that application's user ID, and not normally accessible to other packages. When creating a new file with getSharedPreferences(String, int), openFileOutput(String, int), or openOrCreateDatabase(String, int, SQLiteDatabase.CursorFactory), you can use the MODE_WORLD_READABLE and/or MODE_WORLD_WRITEABLE flags to allow any other package to read/write the file. When setting these flags, the file is still owned by your application, but its global read and/or write permissions have been set appropriately so any other application can see it.
You cannot break up the su and the chmod operations like that.
This code:
Runtime.getRuntime().exec("su");
Runtime.getRuntime().exec("chmod 777 " + file.getAbsolutePath());
Does not result in the chmod being executed in a root shell. Each call to exec kicks off a NEW process.
You need to run the commands you need all within a single process. The easiest way to do that is to write a shell script to your /data/data directory that does these operations for you and then run that through the sh shell processor.
Please note that good security practice would be to chmod the file back to not world readable after you are done with it in your app so that you are not leaving the other app exposed forever.
This answer looks to have what you need: Run binary from with root Android Application
SOLVED
Solved using this code to run the commands:
public static void runAsRoot(String[] cmds){
try {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(p.getOutputStream());
for (String tmpCmd : cmds) {
os.writeBytes(tmpCmd+"\n");
}
os.writeBytes("exit\nexit\n");
os.flush();
p.waitFor();
} catch(IOException e) {
e.printStackTrace();
} catch(InterruptedException e) {
e.printStackTrace();
}
}

How to create a file of specific size on sd card programatically

Can some one please let me know how to create a file of specific size on sd card programatically.
I did tried shell command like this which is of no use,
try {
Process p = Runtime.getRuntime().exec("dd of=/mnt/extSdCard/output.dat bs=1 seek=1M count=0");
} catch (IOException e1) {
}
I did even try the same command in shell script and was not able to create the file.
I guess since my device is not rooted, this command did not work.
Any help regarding this highly appreciated.
-regards,
Manju
you can use RandomAccessFile as shown here.
First you obtain prermission for External storage.
Edit the AndroidManifest.xml with the following line of code.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
After that in java file write the following code.
File fileName = new File(Environment.getExternalStorageDirectory(),
"output.dat");
FileOutputStream fos = new FileOutputStream(fileName )
fos.write(bytes);

Permission denied: File created in .../files

I'm creating a file in data/data/myPackage/files/ :
file = new File( getFilesDir() + "/file.txt");
I'm absolutely sure that the file is created.
Right after its creation I call:
file.canWrite();
and the result is true.
When I try to use that file
I get: "Permission Denied".
In Eclipse, in DDMS, this file permissions are like:
-rw-------
Can anyone help here?
Thanks
Ensure you have the correct permissions to write to the file. In your manifest file, include this line
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />.
Easiest way to open the file (and create it at the same time) would be to use the openFileOutput("file.txt", MODE_PRIVATE) method.
This ensures that the file is only readable by your application (the same as you've have at the moment), plus it returns you a FileOutputStream so you can start writing to it.
The permissions look correct to me. Each application runs as a different user so only has access to it's own files.
When you say "I try to use that file" what do you mean? Can you post the code for that as I think that is where the problem lies.
If you know the file is being created using File() and you don't want to use openFileOutput() then try this:
FileWriter fWriter;
try {
fWriter = new FileWriter(file, true);
fWriter.write("hello");
fWriter.close();
} catch (IOException e) {
// log error
}
I have been working on the problem of nullpointerexception, file not found, for a few hours. I have moved from the emulator to an actual device. In the emulator the following worked:
BufferedWriter osw = new BufferedWriter(new FileWriter(path));
When I went directly to the device though, I got the exception.
Neelesh Gajender has hit the answer perfectly. Adding: :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in the android manifest solves this problem completely. I am grateful to Neelesh and Kudos!
I've found that for the writing on '/data/data/myPackage/files/',
permissions need to be set.
There was MODE_WORLD_WRITEABLE,
that was the one with interest for this case,
but even with this permission set, I still got error.
I thought: "maybe what I need is an EXECUTE permission...".
There is no such permission in the API
but even though I tried to put a 3 in the mode parameter
(MODE_WORLD_WRITEABLE is = 2),
and...it worked!
But when I write to the sd card with that permission set,
I get an error as well,
so I write differently to the two folders.
int permission = 3;
if (whereToWrite == WRITE_TO_DATA_FOLDER) {
FileOutputStream fos = openFileOutput(file.getName(), permission);
buffOutStream = new BufferedOutputStream(fos);
} else if (whereToWrite == WRITE_TO_SD_CARD){
buffOutStream = new BufferedOutputStream(new FileOutputStream(file));
}
And by the way,
MODE_WORLD_WRITEABLE is wrong,
MODE_WORLD_WRITABLE is right.

Categories

Resources