我想将图像从URL保存到SD卡(以备将来使用),然后从SD卡加载该图像,将其用作Google地图的可绘制叠加层.
这是函数的保存部分:
//SAVE TO FILE
String filepath = Environment.getExternalStorageDirectory().getAbsolutePath();
String extraPath = "/Map-"+RowNumber+"-"+ColNumber+".png";
filepath += extraPath;
FileOutputStream fos = null;
fos = new FileOutputStream(filepath);
bmImg.compress(CompressFormat.PNG, 75, fos);
//LOAD IMAGE FROM FILE
Drawable d = Drawable.createFromPath(filepath);
return d;
Run Code Online (Sandbox Code Playgroud)
图像以succuessfully的形式保存到SD卡,但在到达createFromPath()线路时失败.我不明白为什么它会保存到那个目的地但不加载它....
我想在我的代码中使用按钮保存URL中的图像我已经创建了目标文件夹,但我尝试了各种代码来保存图片但是什么都不起作用:
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 …Run Code Online (Sandbox Code Playgroud)