从url下载图像并将其显示到真实设备库android?
使用此代码从内部存储中的url下载图像。它工作正常,并在模拟器中显示图像,但在实际设备上调试时,它给出的是空的。。帮帮我从url下载图像并将其显示到真实设备库android?,android,memory,internal,Android,Memory,Internal,使用此代码从内部存储中的url下载图像。它工作正常,并在模拟器中显示图像,但在实际设备上调试时,它给出的是空的。。帮帮我 File fileWithinMyDir = getApplicationContext().getFilesDir(); downloadImage(url1, fileWithinMyDir.getAbsolutePath()+MODE_WORLD_READABLE + "/" +"image1.jpg"); public boolean downloadImage(S
File fileWithinMyDir = getApplicationContext().getFilesDir();
downloadImage(url1, fileWithinMyDir.getAbsolutePath()+MODE_WORLD_READABLE + "/" +"image1.jpg");
public boolean downloadImage(String URL,String fileName){
try
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
URL url = new URL(URL);
File file = new File(fileName);
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1)
{
baf.append((byte) current);
}
FileOutputStream fos = new FileOutputStream(file,true);
fos.write(baf.toByteArray());
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}
return true;
}
如果您的设备已通过USB电缆连接,请运行应用程序并从USB电缆中卸下设备并进行检查。我希望它能起作用。 使用休耕代码也不会起作用
String serverURL1 = "enter your image url";
new imageFile().execute(serverURL1);
public class imageFile extends AsyncTask<String,Void,Void>
{
private Context context;
public void setContext(Context contextf)
{
context = contextf;
}
protected Void doInBackground(String... arg0)
{
try
{
URL url = new URL(arg0[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.connect();
File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"SD card path");
FileOutputStream f = new FileOutputStream(myFile);
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
while ( (len1 = in.read(buffer)) > 0 )
{
f.write(buffer,0, len1);
}
f.close();
}
catch (Exception e)
{
Log.e("imageFile", "Update error! " + e.getMessage());
}
return null;
}}
//Animation class focus on windows button change
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
if (hasFocus)
{
frameAnimation.start();
}
else
{
frameAnimation.stop();
}
}
String serverURL1=“输入图像url”;
新建imageFile().execute(serverURL1);
公共类imageFile扩展异步任务
{
私人语境;
公共void setContext(Context contextf)
{
上下文=上下文f;
}
受保护的Void doInBackground(字符串…arg0)
{
尝试
{
URL=新URL(arg0[0]);
HttpURLConnection c=(HttpURLConnection)url.openConnection();
c、 setRequestMethod(“GET”);
c、 connect();
File myFile=新文件(Environment.getExternalStorageDirectory().getAbsolutePath(),“SD卡路径”);
FileOutputStream f=新的FileOutputStream(myFile);
InputStream in=c.getInputStream();
字节[]缓冲区=新字节[1024];
int len1=0;
而((len1=in.read(buffer))>0)
{
f、 写入(缓冲区,0,len1);
}
f、 close();
}
捕获(例外e)
{
Log.e(“imageFile”,“更新错误!”+e.getMessage());
}
返回null;
}}
//动画类关注windows按钮更改
WindowFocusChanged上的公共无效(布尔hasFocus)
{
super.onWindowFocusChanged(hasFocus);
如果(hasFocus)
{
frameAnimation.start();
}
其他的
{
frameAnimation.stop();
}
}
首先,我强烈建议使用Asynctask
而不是在主线程中使用StrictMode
执行此操作,因为我使用的是StrictMode.ThreadPolicy policy=new StrictMode.ThreadPolicy.Builder().permitAll().build();StrictMode.setThreadPolicy(策略);。。我将使用asynctask,但现在这不是我的问题。有什么解决方案吗?您是否在清单文件中添加了该任务所需的权限。