Reputation: 317
I have a chrome packaged app that uses the chrome.fileSystem
API to access a user's directory. It displays all files in the directory and the user can open and edit text files in the app.
It displays all files even not-text files (e.g. images) and I want that the user can click on these, too, but they should open in the Chrome OS standard image viewer.
Is there a chrome API to do this, like a function that I can pass the URL of the file or the file entry to, and then the system performs the standard operation?
Upvotes: 2
Views: 359
Reputation: 3740
The answer to your direct question is no. Instead you can do one of these things:
Take the file name's extension, and show the file appropriately based on that.
Call chrome.mediaGalleries.getMetadata
to determine the media type, and base the action on that.
The following code, taken from a sample app I put together for my book, doesn't handle the general case, as the files it deals with are only images, video, or audio, but it provides some guidance as to how this API is used:
chrome.mediaGalleries.getMetadata(file, {},
function (metadata) {
if (metadata && metadata.mimeType) {
var element;
var mediaType = metadata.mimeType.split('/')[0];
var elementName = mediaType === 'image' ? 'img' : mediaType;
element = document.createElement(elementName);
element.setAttribute("controls", "controls");
viewDiv.appendChild(element);
element.style['max-width'] = '700px';
element.style['max-height'] = '700px';
element.src = URL.createObjectURL(file);
}
}
);
URL.createObjectURL
directly to another window or a webview within the app's window.Note also that you can't access what you call the Chrome OS standard image viewer from within a Chrome App.
Upvotes: 2