[Android Apps and Games] arnaud42:
Hi,

I take my code from directly from Google (Bookmarks import/export are already implemented in Chrome), here to be specific:

https://cs.chromium.org/chromium/sr...okmarks_api.cc?sq=package:chromium&dr=C&l=778

I'm ok to share the code. The more the browsers support this functionality, the better (so users can bring their bookmarks from one place to another).

Here you go:

chrome/browser/android/bookmarks/bookmark_bridge.cc

void BookmarkBridge::FileSelected(const base::FilePath& path,
int index,
void* params) {
ExternalProcessImporterHost* importer_host = new ExternalProcessImporterHost;
importer::SourceProfile source_profile;
source_profile.importer_type = importer::TYPE_BOOKMARKS_FILE;
source_profile.source_path = path;
importer_host->StartImportSettings(source_profile,
GetProfile(),
importer::FAVORITES,
new ProfileWriter(GetProfile()));
}

void BookmarkBridge::ImportBookmarks(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

ui::SelectFileDialog::Type type = SELECT_OPEN_FILE

WebContents* web_contents = GetSenderWebContents();

select_file_dialog_ = ui::SelectFileDialog::Create(
this, std::make_unique<ChromeSelectFilePolicy>(web_contents));
ui::SelectFileDialog::FileTypeInfo file_type_info;
file_type_info.extensions.resize(1);
file_type_info.extensions[0].push_back(FILE_PATH_LITERAL("html"));
gfx::NativeWindow owning_window =
web_contents ? platform_util::GetTopLevel(web_contents->GetNativeView())
: gfx::kNullNativeWindow;
// |web_contents| can be NULL (for background pages), which is fine. In such
// a case if file-selection dialogs are forbidden by policy, we will not
// show an InfoBar, which is better than letting one appear out of the blue.
select_file_dialog_->SelectFile(type,
base::string16(),
default_path,
&file_type_info,
0,
base::FilePath::StringType(),
owning_window,
NULL);
}

void BookmarkBridge::ExportBookmarks(JNIEnv* env,
const JavaParamRef<jobject>& obj) {
DCHECK_CURRENTLY_ON(content::BrowserThread::UI);

chrome::GetUserDownloadsDirectory(&file_path_);
file_path_ = file_path_.Append(FILE_PATH_LITERAL("bookmarks.html"));

bookmark_html_writer::WriteBookmarks(profile_, file_path_, NULL);

Java_BookmarkBridge_bookmarksExported(env, obj, ConvertUTF8ToJavaString(env, file_path_.MaybeAsASCII()));
}

chrome/android/java/src/org/chromium/chrome/browser/bookmarks/BookmarkBridge.java
[MENTION=6778970]called[/MENTION]ByNative
public void bookmarksImported(String message) {
Context context = ContextUtils.getApplicationContext();

Toast.makeText(ContextUtils.getApplicationContext(), message, Toast.LENGTH_LONG).show();
}

[MENTION=6778970]called[/MENTION]ByNative
public void bookmarksExported(String bookmarksPath) {
String url = bookmarksPath;

Context context = ContextUtils.getApplicationContext();

url = "file://" + url;

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.putExtra(Browser.EXTRA_APPLICATION_ID,
context.getPackageName());
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(IntentHandler.EXTRA_PAGE_TRANSITION_TYPE, PageTransition.AUTO_BOOKMARK);

// If the bookmark manager is shown in a tab on a phone (rather than in a separate
// activity) the component name may be null. Send the intent through
// ChromeLauncherActivity instead to avoid crashing. See crbug.com/615012.
intent.setClass(context, ChromeLauncherActivity.class);

IntentHandler.startActivityForTrustedIntent(intent);
}

For the notification, I'm using a android.widget.Toast (you can get it to show anywhere by getting context with ContextUtils.getApplicationContext() without having to pass any reference)