从非 UI 线程保存文件时 Android 权限被拒绝

发布于 2024-12-05 09:20:02 字数 3424 浏览 1 评论 0原文

我在从新线程下载图像并将其保存到文件中时遇到问题。

在某些设备上,它

在其他设备上工作得很好,例如 HTC 难以置信,我遇到了一个异常,

我设法弄清楚它在这一行中:

InputStream is=new URL(url).openStream();

这是完整的堆栈转储

import java.io.InputStream;

Bitmap bitmap=null;
InputStream is=new URL(url).openStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();


D/dalvikvm( 1930): GC_EXPLICIT freed 26K, 47% free 3642K/6855K, external 0K/0K, paused 90ms
W/System.err( 2669): java.io.FileNotFoundException: /mnt/sdcard/com.example.testing1/remote-image-cache/74110ca30e217cfdcc78a98f814de093c217bc07.hdr (Permission denied)
W/System.err( 2669):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
W/System.err( 2669):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
W/System.err( 2669):    at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
W/System.err( 2669):    at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
W/System.err( 2669):    at java.io.FileWriter.<init>(FileWriter.java:42)
W/System.err( 2669):    at org.appcelerator.titanium.util.TiResponseCache.put(TiResponseCache.java:359)
W/System.err( 2669):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.maybeCache(HttpURLConnectionImpl.java:331)
W/System.err( 2669):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1043)
W/System.err( 2669):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
W/System.err( 2669):    at java.net.URL.openStream(URL.java:645)
W/System.err( 2669):    at ti.testing.ImageLoader.getBitmap(ImageLoader.java:163)
W/System.err( 2669):    at ti.testing.ImageLoader.access$100(ImageLoader.java:29)
W/System.err( 2669):    at ti.testing.ImageLoader$PhotosLoader.run(ImageLoader.java:256)

我的manifest.xml文件包含许多权限:

</application>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

这个这就是我获取文件路径的方式,

   if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
    else
        cacheDir=context.getCacheDir();

我不明白......想法?

编辑

我尝试更改:

cacheDir=new File("/mnt/","emmc"); 

但现在我得到:

W/System.err( 404): java.io.FileNotFoundException: /mnt/emmc/cp_1216407347.jpeg 
(No such file or directory)

我尝试更改cacheDir=new File("/mnt/","emmc");但现在我得到 W/System.err( 404): java.io.FileNotFoundException: /mnt/emmc/cp_1216407347.jpeg (没有这样的文件或目录)

I having trouble with the download of an image from a new thread and saving it into a file.

on some devices it works flawelsly

on others, such as the HTC INCERDIBLE, I am getting an exception

I managed to figure out it is in this line:

InputStream is=new URL(url).openStream();

this is the complete stack dump

import java.io.InputStream;

Bitmap bitmap=null;
InputStream is=new URL(url).openStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();

D/dalvikvm( 1930): GC_EXPLICIT freed 26K, 47% free 3642K/6855K, external 0K/0K, paused 90ms
W/System.err( 2669): java.io.FileNotFoundException: /mnt/sdcard/com.example.testing1/remote-image-cache/74110ca30e217cfdcc78a98f814de093c217bc07.hdr (Permission denied)
W/System.err( 2669):    at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
W/System.err( 2669):    at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
W/System.err( 2669):    at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
W/System.err( 2669):    at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
W/System.err( 2669):    at java.io.FileWriter.<init>(FileWriter.java:42)
W/System.err( 2669):    at org.appcelerator.titanium.util.TiResponseCache.put(TiResponseCache.java:359)
W/System.err( 2669):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.maybeCache(HttpURLConnectionImpl.java:331)
W/System.err( 2669):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.retrieveResponse(HttpURLConnectionImpl.java:1043)
W/System.err( 2669):    at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:512)
W/System.err( 2669):    at java.net.URL.openStream(URL.java:645)
W/System.err( 2669):    at ti.testing.ImageLoader.getBitmap(ImageLoader.java:163)
W/System.err( 2669):    at ti.testing.ImageLoader.access$100(ImageLoader.java:29)
W/System.err( 2669):    at ti.testing.ImageLoader$PhotosLoader.run(ImageLoader.java:256)

My manifest.xml file includes many permissions:

</application>

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>

this is how I am getting the file path

   if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
        cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"LazyList");
    else
        cacheDir=context.getCacheDir();

I don't understand it... Ideas ?

EDIT

I tried changing:

cacheDir=new File("/mnt/","emmc"); 

but now I get:

W/System.err( 404): java.io.FileNotFoundException: /mnt/emmc/cp_1216407347.jpeg 
(No such file or directory)

I tried changing cacheDir=new File("/mnt/","emmc"); but now I get W/System.err( 404): java.io.FileNotFoundException: /mnt/emmc/cp_1216407347.jpeg (No such file or directory)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文