当我尝试打开文件时,应用程序崩溃了.它可以在Android Nougat下运行,但在Android Nougat上它会崩溃.当我尝试从SD卡打开文件而不是从系统分区打开文件时,它只会崩溃.一些许可问题?
示例代码:
File file = new File("/storage/emulated/0/test.txt");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); // Crashes on this line
Run Code Online (Sandbox Code Playgroud)
日志:
android.os.FileUriExposedException:file:///storage/emulated/0/test.txt通过Intent.getData()暴露在app之外
编辑:
在定位Android Nougat时,file://
不再允许使用URI.我们应该使用content://
URI代替.但是,我的应用程序需要打开根目录中的文件.有任何想法吗?
我正进入(状态
开放失败:
EACCES (Permission denied)
在线上 OutputStream myOutput = new FileOutputStream(outFileName);
我检查了根,我试过了android.permission.WRITE_EXTERNAL_STORAGE
.
我该如何解决这个问题?
try {
InputStream myInput;
myInput = getAssets().open("XXX.db");
// Path to the just created empty db
String outFileName = "/data/data/XX/databases/"
+ "XXX.db";
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// Transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the …
Run Code Online (Sandbox Code Playgroud) android android-permissions android-5.0-lollipop android-storage
我正在网上搜索如何在android上使用默认的pdf viewer从服务器打开pdf文件.我找到的是首先下载文件然后在意图中启动它或使用谷歌文档加载它.我不想做所有这些.我只是想从手机的默认pdf查看器直接从服务器加载它.我已经尝试打开视频网址并且有效.但是故意打开pdf网址是行不通的.以下是我的代码;
private void openFilePDF(){
try{
Toast.makeText(getBaseContext(), "Opening PDF... ", Toast.LENGTH_SHORT).show();
Intent inte = new Intent(Intent.ACTION_VIEW);
inte.setDataAndType(
Uri.parse("http://122.248.233.68/pvfiles/Guide-2.pdf"),
"application/pdf");
startActivity(inte);
}catch(ActivityNotFoundException e){
Log.e("Viewer not installed on your device.", e.getMessage());
}
}
Run Code Online (Sandbox Code Playgroud)
有什么方法可以加载pdf网址吗?
我试图从外部URL打开PDF文件.代码是下一个:
private void cargaPdf(Uri url){
//File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(url,"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Intent intent = Intent.createChooser(target, "Open File");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
// Instruct the user to install a PDF reader here, or something
}
}
Run Code Online (Sandbox Code Playgroud)
我已经安装了adobe和google PDF查看器但是当我按下这个url调用"cargaPdf(Uri url)"函数的按钮时(你可以看到是一个pdf文件):
Uri.parse(https://dl.dropboxusercontent.com/s/oulzgpgnq2ca1ly/KorfbalSpa.pdf?dl=0);
Run Code Online (Sandbox Code Playgroud)
它出现在屏幕上的西班牙语"打开文件Ninguna aplicacion puede realizar esta accion"或英语"打开文件没有应用程序可以执行此操作".发生什么事?
编辑1:好的,现在我知道我必须下载文件才能看到它,所以,我在活动中做了这个代码:
public void cargaDescripcion(View view){
Log.d("CargaDescripcion", "Antes del directorio");
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(extStorageDirectory, "Mypdf");
folder.mkdir();
File …
Run Code Online (Sandbox Code Playgroud) 我正在尝试下载一个简单的.pdf
文件,我尝试过日志,但没有错误也没有。我试图调试,但直到昨天它仍然像魅力一样工作,但今天不是,我没有对代码的这一部分进行任何更改。我基于这个问题和答案在 SO。
下面是代码。
有时在 LogCat 它只显示我喜欢这样的消息。
com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:251) FileDownloader.downloadFile
在这行代码。
inputStream = urlConnection.getInputStream();
Run Code Online (Sandbox Code Playgroud)
Url 连接的代码。
urlConnection.getResponseCode()
它正在返回 405
MainActivity.java
String newEntry = "http://www.axmag.com/download/pdfurl-guide.pdf";
new DownloadFile().execute(newEntry, "maven.pdf");
private static class DownloadFile extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
String fileUrl = strings[0]; // -> http://maven.apache.org/maven-1.x/maven.pdf
String fileName = strings[1]; // -> maven.pdf
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "testthreepdf");
folder.mkdir();
File pdfFile = new File(folder, fileName);
try{
pdfFile.createNewFile();
}catch (IOException e){
e.printStackTrace();
} …
Run Code Online (Sandbox Code Playgroud)