can we get chrome browsing history/bookmarks in our android app - android

can we get chrome browsing history/bookmarks like we get in default browser using READ_HISTORY_BOOKMARKS permission?
PS:I Just want to know is it possible?

Yes it is very much possible.
Use this uri: content://com.android.chrome.browser/bookmarks instead of Browser.BOOKMARKS_URI
String[] proj = new String[] { Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL };
Uri uriCustom = Uri.parse("content://com.android.chrome.browser/bookmarks");
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(uriCustom, proj, sel, null, null);
mCur.moveToFirst();
#SuppressWarnings("unused")
String title = "";
#SuppressWarnings("unused")
String url = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
boolean cont = true;
while (mCur.isAfterLast() == false && cont) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCur.moveToNext();
}
}
Havent tested the code for errors but it should work fine. The important thing is knowing the uri to use. Reading this and this might help.

Related

How to get the browsing history in android for a particular time period

I would like to develop an android app for accessing the history of the browser with in a certain time interval by using AlaramManager, but when I access the history infomation, i'm getting the new information along with the previous history. Is there any way to to get the history of last few(5 or 10) minutes
Below is the code for accesing the browsing history
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL , Browser.BookmarkColumns.DATE};
Uri uriCustom = Uri.parse("content://com.android.chrome.browser/bookmarks");
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(uriCustom, proj, sel, null, null);
//this.startManagingCursor(mCur);
mCur.moveToFirst();
String title = "";
String url = "";
String date_time="";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (mCur.isAfterLast() == false) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
date_time=mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.DATE));
// Do something with title and url
System.out.println("Title="+title+"---"+url);
mCur.moveToNext();
}
}
Any help appreciated!
Append " AND " + Browser.BookmarkColumns.DATE + "BETWEEN ? AND ?" to your sel variable and pass new String[]{startdate, enddate} in selectionArgs of query method.
Modify the startdate and enddate as per your requirements. This method will return the data between to specific time period. We are just making sql query to get data between two particular time period. Ex :
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0 " +" AND " + Browser.BookmarkColumns.DATE + "BETWEEN ? AND ?"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(uriCustom, proj, sel, new String[]{startdate, enddate}, null);

How to retrieve and display browser history and bookmarks in browser application [duplicate]

This question already has answers here:
Get browser history and search result in android
(2 answers)
Closed 8 years ago.
Hi friends i am new to android I don't know how to retrieve and display history in web browser.
Please guide me and share your thoughts, ideas and bookmarks too.
I have to display history in ListView
You can use getContentResolver to get the browser history as managedQuery has been depricated.
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj, sel, null, null);
mCur.moveToFirst();
String title = "";
String url = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
boolean cont = true;
while (mCur.isAfterLast() == false && cont) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCur.moveToNext();
}
}
Also add permissions in your manifest
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
For more details to access data using Cursor check Access Data using Cursor
ArrayList mTitles=new ArrayList();
ArrayList mUrls=new ArrayList();
public void getBrowserHist() {
Cursor mCur = managedQuery(Browser.BOOKMARKS_URI,
Browser.HISTORY_PROJECTION, null, null, null);
mCur.moveToFirst();
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (mCur.isAfterLast() == false) {
Log.v("titleIdx", mCur
.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
Log.v("urlIdx", mCur
.getString(Browser.HISTORY_PROJECTION_URL_INDEX));
mCur.moveToNext();
}
}
}
Add below uses-permission into your manifest file.
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
Enjoy :-)

Android API- getting browser bookmarks/history

I'd like to write a small android widget to getting bookmarks from my firefox browser, but my code:
Cursor myCursor=Browser.getAllBookmarks(main.getContentResolver());
not working. This cursor is always empty. It is very strange, because all my browsers (firefox, chrome) have a lot of bookmarks.
I have found this code:
String query = Browser.BookmarkColumns.BOOKMARK+"=1";
Cursor crs=main.getContentResolver().query( UriProvider.QUERY
, columns
, query
, null
, sortOrder
);
but efect is the same, result is empty.
I have problem with understanding nature of android.provider.Browser class. Is it a interface to database table ? What kind of data I can find in this table (bookmarks from firefox, or chrome, or both ???). When this table is synchronized with ff/chrome ?
Thanks for any suggestions...
Best regards
mario
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 =bookmark
mCur = this.managedQuery(Browser.BOOKMARKS_URI, proj, sel, null, null);
this.startManagingCursor(mCur);
mCur.moveToFirst();
String title = "";
String url = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
while (mCur.isAfterLast() == false && cont) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCur.moveToNext();
}
}
have you added permission in AndroidManifest.xml
<uses-permission
android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS"/>
Thank you for reply.
Your code works but only for default build-in browser.
For chrome I have to use special content provider uri:
Uri uriCustom = Uri.parse("content://com.android.chrome.browser/bookmarks");
For FF the thing is more complicated, because when I try use uri:
Uri uriCustom = Uri.parse("content://org.mozilla.firefox.db.browser/bookmarks");
I get :
java.lang.SecurityException: Permission Denial: reading org.mozilla.firefox.db.BrowserProvider uri content://org.mozilla.firefox.db.browser/bookmarks from pid=xxxx, uid=xxxx requires org.mozilla.firefox.permissions.BROWSER_PROVIDER, or grantUriPermission()

How to get imagepath from thumbnail path of a image?

I am trying to get imagepath based on thumbail path , i have already tried solution from
android-getting-path-to-image-from-thumbnail, but it is based on gridview position, i am only retrieving specific images. Also i found one sample code from SO, the code is
private String getImagePathFromThumbPath(String thumbPath)
{
String imgPath=null;
// String[] projection = new String[] {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID};
String[] imagesDataPath={ MediaStore.Images.Media.DATA ,MediaStore.Images.Media._ID};
//mResolver.query() requires android API 16
Cursor thumbnails = mResolver.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, imagesDataPath,MediaStore.Images.Thumbnails.DATA+"=?",new String[]{thumbPath}, null, null);
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor imageCursor = mResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, MediaStore.Images.Media._ID + "=?", new String[] {thumbId}, null);
if (imageCursor != null && imageCursor.moveToFirst()) {
// Your file-path will be here
imgPath= imageCursor.getString(imageCursor.getColumnIndex(filePathColumn[0]));
}
return imgPath;
}
The above method is bit modified to suit my needs and it returns nothing on Toasting, please tell how to retrieve imagepath using thumbnail path?
After a long time and relentless try, the solution is here
1. You need to find the image id which is image unique id from images table in thumbnail table, query to thumbnail provider (MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI) if you do not understand it, refer here , specifically IMAGE_ID,
Step 1 is to get retrievedImageId.
retrievedImageId = Long.parseLong(cursor.getString(imageIdInImages));
2. Now using the retrievedImageId get the image path, by again querying the content provider, only this time query the Images media provider (MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
String getImagePathFromThumbPath(String thumbPath)
{
String imagePath = null;
if(thumbPath != null )
{
String[] columns_to_return = {MediaStore.Images.Thumbnails.IMAGE_ID};
String where = MediaStore.Images.Thumbnails.DATA+" LIKE ?";
long retrievedImageId = -1;
String valuesAre[] = {"%"+thumbPath};
Cursor cursor = getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, columns_to_return, where, valuesAre, null);
if(cursor != null)
{
int imageIdInImages = cursor.getColumnIndex(MediaStore.Images.Thumbnails.IMAGE_ID);
for (cursor.moveToFirst();!cursor.isAfterLast(); cursor.moveToNext())
{
//STEP 1 to retrieve image ID
retrievedImageId = Long.parseLong(cursor.getString(imageIdInImages));
}
if(retrievedImageId != -1)
{
// STEP 2 Now
Log.i(TAG, "imageId-" + retrievedImageId);
String[] columnsReturn = {MediaStore.Images.Media.DATA};
String whereimageId = MediaStore.Images.Media._ID+" LIKE ?";
String valuesIs[] = {"%" + retrievedImageId};
Cursor mCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columnsReturn, whereimageId, valuesIs, null);
int rawDataPath= mCursor.getColumnIndex(MediaStore.Images.Media.DATA);
for (mCursor.moveToFirst();!mCursor.isAfterLast(); mCursor.moveToNext())
{
imagePath = mCursor.getString(rawDataPath);
}
}
}
}
return imagePath;
}
If you still have doubt or error/exception, leave comment!

Android read browser history

I want to read browser history in Android phone.
I have done some document reading, then I come to know that we can read browser history by android.provider.Browser class. It has :
final static Cursor
getAllVisitedUrls(ContentResolver cr)
...method which returns Cursor.
May I get help to handle Cursor, or any example code to get browser history?
Not really an answer but I can tell you what I did.
I first clone the browser repo and try to reproduce how they get the history.
And I started getting:
Permission Denial: reading
com.android.browser.BrowserProvider
So I added:
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
But it still is giving me the same error. I google it and I found this Accessing Data With Android Cursors.
Hope it helps.
managedQuery has been deprecated so use getContentResolver instead, use the following code:
String[] proj = new String[] { Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history, 1 = bookmark
Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj, sel, null, null);
mCur.moveToFirst();
#SuppressWarnings("unused")
String title = "";
#SuppressWarnings("unused")
String url = "";
if (mCur.moveToFirst() && mCur.getCount() > 0) {
boolean cont = true;
while (mCur.isAfterLast() == false && cont) {
title = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
mCur.moveToNext();
}
}
Also add permissions using
<uses-permission android:name="com.android.browser.permission.READ_HISTORY_BOOKMARKS" />
For Lollipop or earlier
I am able to get the history by using the following code:
Cursor mCur = activity.managedQuery(Browser.BOOKMARKS_URI,
Browser.HISTORY_PROJECTION, null, null, null);
if (mCur.moveToFirst()) {
while (mCur.isAfterLast() == false) {
Log.v("titleIdx", mCur
.getString(Browser.HISTORY_PROJECTION_TITLE_INDEX));
Log.v("urlIdx", mCur
.getString(Browser.HISTORY_PROJECTION_URL_INDEX));
mCur.moveToNext();
}
}
This post is a little bit old, but here is another easy solution for getting data related to Bookmark and Search content providers in Android:
Use this lib: https://github.com/EverythingMe/easy-content-providers
Get all bookmarks:
BrowserProvider browserProvider = new BrowserProvider(context);
List<Bookmark> bookmarks = browserProvider.getBookmarks().getList();
Each Bookmark has all fields, so you can get any info you need:
title, url, visits, ...
Get all Search history:
List<Search> searches = browserProvider.getSearches().getList();
It works with lists or cursor and there a sample app to see how it looks and works.
In fact, there is support for all Android content providers like: Contacts, SMS, Calls, ...
Full doc with all options: https://github.com/EverythingMe/easy-content-providers/wiki/Android-providers
Hope it helped :)
public ArrayList<HistoryEntry> getBrowserHistory() {
String title = "";
String url = "";
ArrayList<HistoryEntry> list = new ArrayList<HistoryEntry>();
String[] proj = new String[] { Browser.BookmarkColumns.TITLE,
Browser.BookmarkColumns.URL };
String sel = Browser.BookmarkColumns.BOOKMARK + " = 0"; // 0 = history,
// 1 = bookmark
Cursor mCur = getContentResolver().query(Browser.BOOKMARKS_URI, proj,
sel, null, null);
mCur.moveToFirst();
if (mCur.moveToFirst() && mCur.getCount() > 0) {
boolean cont = true;
while (mCur.isAfterLast() == false && cont) {
HistoryEntry entry = new HistoryEntry();
title = mCur.getString(mCur
.getColumnIndex(Browser.BookmarkColumns.TITLE));
url = mCur.getString(mCur
.getColumnIndex(Browser.BookmarkColumns.URL));
// Do something with title and url
entry.setTitle(title);
entry.setUrl(url);
list.add(entry );
Log.d("TAG", "title " + title);
mCur.moveToNext();
}
}
mCur.close();
return list;
}

Categories

Resources