Helo,朋友我试图使用绑定服务从互联网上下载一个图像,通过下载方法我调用绑定服务类,并通过这个服务调用一个asyctask类来通过doInBackground显示doenload进度(URL .. 。urls)方法。但是当output = new FileOutputStream(Path+"hive.jpg");
被调用时,错误java.io.FileNotFoundException: /mnt/sdcard/hive.jpg: open failed: EACCES (Permission denied)
发生错误。请快速帮助,提前谢谢。
我取得了所有必要的许可。我的menifist文件许可:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
我的背景方法代码如下:
@Override
protected Long doInBackground(URL... urls) {
int count;
try {
System.out.println("do in backgrounds");
URL url = new URL(urls[0].toString());
System.out.println("utl=" + url);
URLConnection conexion = url.openConnection();
System.out.println("open connecton+" + conexion);
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
System.out.println("input stream=" + input);
String Path = Environment.getExternalStorageDirectory()
+ File.separator;// "/sdcard/";
System.out.println("PAth=" + Path);
try {
output = new FileOutputStream(Path+"hive.jpg"); // exception error here
System.out.println("output stream=" + output);
} catch (Exception e) {
e.printStackTrace();
}
// OutputStream output = new FileOutputStream(Path+"hive.jpg");
byte data[] = new byte[1024];
long total = 0;
System.out.println("before while");
while ((count = input.read(data)) != -1) {
total += count;
int percent = (int) ((total * 100) / lenghtOfFile);
publishProgress(percent);
output.write(data, 0, count);
System.out.println("file write successfulyy");
}
output.flush();
output.close();
input.close();
System.out.println("close function ");
} catch (Exception e) {
}
//}
return null;
}
错误/异常如下:
: java.io.FileNotFoundException: /mnt/sdcard/hive.jpg: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:416)
at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
at java.io.FileOutputStream.<init>(FileOutputStream.java:128)
at java.io.FileOutputStream.<init>(FileOutputStream.java:117)
at com.example.boundservice2.BackgroundDownload.doInBackground(BackgroundDownload.java:63)
at com.example.boundservice2.BackgroundDownload.doInBackground(BackgroundDownload.java:1)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
at libcore.io.IoBridge.open(IoBridge.java:400)
答案 0 :(得分:0)
请将您的代码更改为以下代码,
File file = new File( Path + "hive.jpg" ); // Add this line
output = new FileOutputStream ( file );
if (!file.exists()) // Add this code
{
file.createNewFile();
}