我正在尝试从浏览器历史记录中提取URL。我找到了下面的代码并尝试实现它。但问题是它在BookmarkColumns
,BOOKMARKS_URI
,noicon
中给出了错误。
我在API等级16,23,25中尝试过。但这个错误解决了。它总是说“无法解析符号‘bookmarkColumns’”
请帮我解决这个…
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.Browser;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<String> titles;
private ArrayList<String> urls;
private ArrayList<Bitmap> bitmaps;
private ContentResolver cr;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createLists();
}
protected void onResume() {
super.onResume();
getBH();
showHistoryBookmarks();
}
public void createLists() {
titles = new ArrayList<String>();
urls = new ArrayList<String>();
bitmaps = new ArrayList<Bitmap>();
}
public void getBH() {
Bitmap icon;
cr = getContentResolver();
String order = Browser.BookmarkColumns.DATE + " DESC";
String[] projection = {Browser.BookmarkColumns.TITLE, Browser.BookmarkColumns.URL, Browser.BookmarkColumns.FAVICON};
//String selection=projection[0]+"=?";
//String args[]={"Google"};
Cursor rows = cr.query(Browser.BOOKMARKS_URI, projection, null, null, order);
if (rows.getCount() > 0) {
while (rows.moveToNext()) {
//read title
String title = rows.getString(rows.getColumnIndex(projection[0]));
//read url
String url = rows.getString(rows.getColumnIndex(projection[1]));
//read icon
byte[] bicon = rows.getBlob(rows.getColumnIndex(projection[2]));
if (bicon != null) {
//convert blob image data to Bitmap
icon = BitmapFactory.decodeByteArray(bicon, 0, bicon.length);
} else {
//default icon for history and bookmarks that do not icons
icon = BitmapFactory.decodeResource(getResources(), R.drawable.noicon);
}
//add to lists
addToList(title, url, icon);
}
//close the cursor
rows.close();
}
}
public void addToList(String title, String url, Bitmap bitmap) {
titles.add(title);
urls.add(url);
bitmaps.add(bitmap);
}
public void showHistoryBookmarks() {
ListView l = (ListView) findViewById(R.id.hb_list);
if (l != null) {
if (titles.size() > 0) {
ListAdapterModel aa = new ListAdapterModel(this, R.layout.listlayout, R.id.hbtitle, titles, urls, bitmaps);
l.setAdapter(aa);
} else {
Toast.makeText(this, "This is no bookmark or history.", Toast.LENGTH_SHORT).show();
}
}
}
}
最佳答案
试试这些chrome history API。阅读文档并尝试一下。我没有试过。但思想是不可操作的。