Android 从url保存图像
我想用代码中的按钮保存URL中的图像我已经创建了目标文件夹,但我尝试了各种代码来保存图片,但没有任何效果:Android 从url保存图像,android,eclipse,url,Android,Eclipse,Url,我想用代码中的按钮保存URL中的图像我已经创建了目标文件夹,但我尝试了各种代码来保存图片,但没有任何效果: public class B_X extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState);
public class B_X extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bx);
Button buttonSetWallpaper = (Button)findViewById(R.id.bSetWall);
buttonSetWallpaper.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
File folder = new File(Environment.getExternalStorageDirectory() + "/PerfectAss/");
boolean success = false;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (!success) {
} else {
}
Toast.makeText(getApplicationContext(), "The image has been saved", Toast.LENGTH_LONG).show();
URL url = new URL ("http://bitsparrow.altervista.org/wp-content/uploads/2013/04/5.jpg");
InputStream input = url.openStream();
try {
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (storagePath + "/myImage.png");
try {
byte[] buffer = new byte[1500];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
请帮助我修复此代码。您是否在清单文件中包含权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
您是否在清单文件中包含权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
使用这种方法很简单:将文件保存在sdcard/dhaval_files/中。只需替换您的文件夹名,并授予在android清单文件中写入外部存储的权限
public void file_download(String uRl) {
File direct = new File(Environment.getExternalStorageDirectory()
+ "/dhaval_files");
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir("/dhaval_files", "test.jpg");
mgr.enqueue(request);
}
使用这种方法很简单:将文件保存在sdcard/dhaval_files/中。只需替换您的文件夹名,并授予在android清单文件中写入外部存储的权限
public void file_download(String uRl) {
File direct = new File(Environment.getExternalStorageDirectory()
+ "/dhaval_files");
if (!direct.exists()) {
direct.mkdirs();
}
DownloadManager mgr = (DownloadManager) this.getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(uRl);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle("Demo")
.setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir("/dhaval_files", "test.jpg");
mgr.enqueue(request);
}
如果你在谷歌上搜索同一个问题,你会得到100个链接。那么你到底尝试了什么?因为上面的代码显示了与下载图像并保存图像无关的内容。您没有下载图像,只是检查了PerfectPass目录是否存在,如果不存在,就创建了它。在android上搜索从url获取图像。PS:你的应用程序到底能做什么?您在映像目录中输入了什么样的名称。还记得关于Google Play内容策略吗?我编辑了这个问题,输入了我使用的代码。如果你在Google上搜索同一个问题,你会得到100个链接。那么你到底尝试了什么?因为上面的代码显示了与下载图像并保存图像无关的内容。您没有下载图像,只是检查了PerfectPass目录是否存在,如果不存在,就创建了它。在android上搜索从url获取图像。PS:你的应用程序到底能做什么?您在映像目录中输入了什么样的名称。还记得关于Google Play内容策略吗?我编辑了问题,输入了我使用的代码Dokay尝试上面的代码和fer详细信息检查此链接URL=新URL;eclipse告诉我描述资源路径位置类型未处理的异常类型格式错误异常B_X.javaOK尝试上面的代码和fer详细信息检查此链接URL=newURL;eclipse告诉我描述资源路径位置类型未处理异常类型MalformedURLException B_X.java非常感谢,但我将它放在了下面,但我可以将它放在public void onClickView arg0下?您只需调用此方法。不要把所有代码都放在onclick中。就像:public void onClickView arg0{file_downloadwww.xyz/mobile/img/test.jpg}谢谢,但我想用按钮保存图像,然后保存的图像我必须将其用于另一个活动我怎么能做到呢?非常感谢,但我把它放在下面,但我可以把它放在public void onClickView arg0下?你必须调用这个方法。不要把所有代码都放在onclick中。只需这样做:public void onClickView arg0{file_downloadwww.xyz/mobile/img/test.jpg}谢谢,但我想用按钮保存图像,然后保存的图像我必须将其用于另一个活动如何才能做到这一点?