Titanium 如何在Tianium AppCelerator中使用OpenURL打开本地pdf文件?
在Tianium Appcelerator中,我尝试使用OpenURL()方法从本地目录打开PDF文件。它不能正常工作。我在安卓设备上试过 我的代码Titanium 如何在Tianium AppCelerator中使用OpenURL打开本地pdf文件?,titanium,appcelerator,appcelerator-mobile,Titanium,Appcelerator,Appcelerator Mobile,在Tianium Appcelerator中,我尝试使用OpenURL()方法从本地目录打开PDF文件。它不能正常工作。我在安卓设备上试过 我的代码 var myURL = "file:///storage/emulated/0/Android/data/com.test.testapp/cache/_tmp/sample.pdf"; Ti.Platform.openURL(myURL); 试着这样做: try { var f = Ti.Filesystem.getFile('your.pd
var myURL = "file:///storage/emulated/0/Android/data/com.test.testapp/cache/_tmp/sample.pdf";
Ti.Platform.openURL(myURL);
试着这样做:
try {
var f = Ti.Filesystem.getFile('your.pdf');
Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
action: Ti.Android.ACTION_VIEW,
type: 'application/pdf',
data: f.getNativePath()
}));
}
catch (err) {
var alertDialog = Titanium.UI.createAlertDialog({
title: 'No PDF Viewer',
message: 'We tried to open a PDF but failed. Do you want to search the marketplace for a PDF viewer?',
buttonNames: ['Yes','No'],
cancel: 1
});
alertDialog.show();
alertDialog.addEventListener('click', function(evt) {
if (evt.index == 0) {
Ti.Platform.openURL('http://search?q=pdf');
}
});
}
试着这样做:
try {
var f = Ti.Filesystem.getFile('your.pdf');
Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
action: Ti.Android.ACTION_VIEW,
type: 'application/pdf',
data: f.getNativePath()
}));
}
catch (err) {
var alertDialog = Titanium.UI.createAlertDialog({
title: 'No PDF Viewer',
message: 'We tried to open a PDF but failed. Do you want to search the marketplace for a PDF viewer?',
buttonNames: ['Yes','No'],
cancel: 1
});
alertDialog.show();
alertDialog.addEventListener('click', function(evt) {
if (evt.index == 0) {
Ti.Platform.openURL('http://search?q=pdf');
}
});
}
试着这样做:
try {
var f = Ti.Filesystem.getFile('your.pdf');
Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
action: Ti.Android.ACTION_VIEW,
type: 'application/pdf',
data: f.getNativePath()
}));
}
catch (err) {
var alertDialog = Titanium.UI.createAlertDialog({
title: 'No PDF Viewer',
message: 'We tried to open a PDF but failed. Do you want to search the marketplace for a PDF viewer?',
buttonNames: ['Yes','No'],
cancel: 1
});
alertDialog.show();
alertDialog.addEventListener('click', function(evt) {
if (evt.index == 0) {
Ti.Platform.openURL('http://search?q=pdf');
}
});
}
试着这样做:
try {
var f = Ti.Filesystem.getFile('your.pdf');
Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
action: Ti.Android.ACTION_VIEW,
type: 'application/pdf',
data: f.getNativePath()
}));
}
catch (err) {
var alertDialog = Titanium.UI.createAlertDialog({
title: 'No PDF Viewer',
message: 'We tried to open a PDF but failed. Do you want to search the marketplace for a PDF viewer?',
buttonNames: ['Yes','No'],
cancel: 1
});
alertDialog.show();
alertDialog.addEventListener('click', function(evt) {
if (evt.index == 0) {
Ti.Platform.openURL('http://search?q=pdf');
}
});
}
要以本机方式打开远程PDF,您必须下载它。下面是一个解决方案,它为用户提供预览或下载PDF的选项
var url = "http://www.polyu.edu.hk/iaee/files/pdf-sample.pdf";
var opts = {
cancel: 2,
options: ['Preview', 'Download', 'Cancel'],
selectedIndex: 2,
destructive: 0,
title: 'Open PDF'
};
var dialog = Ti.UI.createOptionDialog(opts);
dialog.addEventListener('click', function(e) {
if (e.index == 0) {
url = "https://docs.google.com/viewer?embedded=true&url=" + url;
var win = Ti.UI.createWindow();
var webView = Ti.UI.createWebView({url:url});
win.add(webView);
win.open();
} else if (e.index == 1) {
var filepath = url.split('/').pop();
var httpClient = Titanium.Network.createHTTPClient({
onload: function() {
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory, filepath);
file.write(this.responseData);
try {
Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
action: Ti.Android.ACTION_VIEW,
type: 'application/pdf',
data: file.getNativePath()
}));
} catch (e) {
alert('No PDF reader found.');
}
}
});
httpClient.open('GET', url);
httpClient.send();
}
});
dialog.show();
要以本机方式打开远程PDF,您必须下载它。下面是一个解决方案,它为用户提供预览或下载PDF的选项
var url = "http://www.polyu.edu.hk/iaee/files/pdf-sample.pdf";
var opts = {
cancel: 2,
options: ['Preview', 'Download', 'Cancel'],
selectedIndex: 2,
destructive: 0,
title: 'Open PDF'
};
var dialog = Ti.UI.createOptionDialog(opts);
dialog.addEventListener('click', function(e) {
if (e.index == 0) {
url = "https://docs.google.com/viewer?embedded=true&url=" + url;
var win = Ti.UI.createWindow();
var webView = Ti.UI.createWebView({url:url});
win.add(webView);
win.open();
} else if (e.index == 1) {
var filepath = url.split('/').pop();
var httpClient = Titanium.Network.createHTTPClient({
onload: function() {
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory, filepath);
file.write(this.responseData);
try {
Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
action: Ti.Android.ACTION_VIEW,
type: 'application/pdf',
data: file.getNativePath()
}));
} catch (e) {
alert('No PDF reader found.');
}
}
});
httpClient.open('GET', url);
httpClient.send();
}
});
dialog.show();
要以本机方式打开远程PDF,您必须下载它。下面是一个解决方案,它为用户提供预览或下载PDF的选项
var url = "http://www.polyu.edu.hk/iaee/files/pdf-sample.pdf";
var opts = {
cancel: 2,
options: ['Preview', 'Download', 'Cancel'],
selectedIndex: 2,
destructive: 0,
title: 'Open PDF'
};
var dialog = Ti.UI.createOptionDialog(opts);
dialog.addEventListener('click', function(e) {
if (e.index == 0) {
url = "https://docs.google.com/viewer?embedded=true&url=" + url;
var win = Ti.UI.createWindow();
var webView = Ti.UI.createWebView({url:url});
win.add(webView);
win.open();
} else if (e.index == 1) {
var filepath = url.split('/').pop();
var httpClient = Titanium.Network.createHTTPClient({
onload: function() {
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory, filepath);
file.write(this.responseData);
try {
Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
action: Ti.Android.ACTION_VIEW,
type: 'application/pdf',
data: file.getNativePath()
}));
} catch (e) {
alert('No PDF reader found.');
}
}
});
httpClient.open('GET', url);
httpClient.send();
}
});
dialog.show();
要以本机方式打开远程PDF,您必须下载它。下面是一个解决方案,它为用户提供预览或下载PDF的选项
var url = "http://www.polyu.edu.hk/iaee/files/pdf-sample.pdf";
var opts = {
cancel: 2,
options: ['Preview', 'Download', 'Cancel'],
selectedIndex: 2,
destructive: 0,
title: 'Open PDF'
};
var dialog = Ti.UI.createOptionDialog(opts);
dialog.addEventListener('click', function(e) {
if (e.index == 0) {
url = "https://docs.google.com/viewer?embedded=true&url=" + url;
var win = Ti.UI.createWindow();
var webView = Ti.UI.createWebView({url:url});
win.add(webView);
win.open();
} else if (e.index == 1) {
var filepath = url.split('/').pop();
var httpClient = Titanium.Network.createHTTPClient({
onload: function() {
var file = Titanium.Filesystem.getFile(Titanium.Filesystem.tempDirectory, filepath);
file.write(this.responseData);
try {
Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
action: Ti.Android.ACTION_VIEW,
type: 'application/pdf',
data: file.getNativePath()
}));
} catch (e) {
alert('No PDF reader found.');
}
}
});
httpClient.open('GET', url);
httpClient.send();
}
});
dialog.show();
我的URL受密码保护,因此如果我在Tianium Appcelerator中尝试,PDF不会在应用程序niether下载中要求密码。你能澄清一下吗?URL是否引用移动设备上的本地路径?或者需要身份验证的https URL?如果是https URL,您可以使用HttpClient代理(并向其传递用户名/密码以下载文件,然后使用上述方法打开。我试图使用远程服务器的URL。我需要的是,当我使用(openURL)打开PDF文件时,它要求输入用户名/密码。当我输入它时,PDF文件被下载到设备内存并从设备存储中打开。我的URL受密码保护,因此,如果我有意在Tianium Appcelerator中尝试,PDF不会在应用程序niether下载中要求输入密码。你能澄清一下吗?URL是否引用了mobil上的本地路径e设备?或需要身份验证的https URL?如果是https URL,您可以使用HttpClient代理(并向其传递用户名/密码以下载文件,然后使用上述方法打开。我尝试使用远程服务器的URL。我需要的是,当我使用(openURL)打开PDF文件时,它要求输入用户名/密码。当我输入它时,PDF文件被下载到设备内存并从设备存储中打开。我的URL受密码保护,因此,如果我有意在Tianium Appcelerator中尝试,PDF不会在应用程序niether下载中要求输入密码。你能澄清一下吗?URL是否引用了mobil上的本地路径e设备?或需要身份验证的https URL?如果是https URL,您可以使用HttpClient代理(并向其传递用户名/密码以下载文件,然后使用上述方法打开。我尝试使用远程服务器的URL。我需要的是,当我使用(openURL)打开PDF文件时,它要求输入用户名/密码。当我输入它时,PDF文件被下载到设备内存并从设备存储中打开。我的URL受密码保护,因此,如果我有意在Tianium Appcelerator中尝试,PDF不会在应用程序niether下载中要求输入密码。你能澄清一下吗?URL是否引用了mobil上的本地路径e设备?或需要身份验证的https URL?如果是https URL,您可以使用HttpClient代理(并向其传递用户名/密码以下载文件,然后使用上述方法打开。我尝试使用远程服务器的URL。我需要的是,当我使用(openURL)打开PDF文件时,它要求输入用户名/密码。当我输入它时,PDF文件被下载到设备内存并从设备存储打开。