Android 图片保存到SDcard中

马小玲 发布于 2012/04/10 18:34
阅读 21K+
收藏 9

【开源中国 APP 全新上线】“动弹” 回归、集成大模型对话、畅读技术报告”

请问各位大侠 如何将drawable中的图片以流的形式保存到SDcard中 如果图片较多时 应该用什么方式保存
加载中
0
fneg
fneg

引用来自“fneg”的答案

class DownLoadItemImageAsync extends
			AsyncTask<TaobaokeItem, Integer, Bitmap> {

		private TaobaokeItem item;
	
		@Override
		protected Bitmap doInBackground(TaobaokeItem... params) {
			item = params[0];
			Bitmap bitmap = getBitmap();
			if (bitmap != null) {
				return bitmap;
			}
			return getItemPic(item.getPicUrl());
		}

		@Override
		protected void onPostExecute(Bitmap result) {
		
			Drawable drawable = new BitmapDrawable(result);
			setProgressBarIndeterminateVisibility(false);
			mImageSwitcher.setImageDrawable(drawable);

		}

		private Bitmap getItemPic(String picUrl) {
			URL url = null;
			URLConnection conn = null;
			InputStream in = null;
			Bitmap itemBitmap = null;
			try {
				url = new URL(picUrl);
				conn = url.openConnection();
				conn.setConnectTimeout(3 * 1000);
				conn.connect();

				in = conn.getInputStream();

				itemBitmap = BitmapFactory.decodeStream(in);
				storeInSD(itemBitmap);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (url != null) {
					url = null;
				}
				if (conn != null) {
					conn = null;
				}
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return itemBitmap;
		}

		private void storeInSD(Bitmap bitmap) {
			File file = new File(FILE_DIR);
			if (!file.exists()) {
				file.mkdir();
			}
			File imageFile = new File(file, item.getNumIid() + ".jpg");
			try {
				imageFile.createNewFile();
				FileOutputStream fos = new FileOutputStream(imageFile);
				bitmap.compress(CompressFormat.JPEG, 50, fos);
				fos.flush();
				fos.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		private Bitmap getBitmap() {
			File imageFile = new File(FILE_DIR, item.getNumIid() + ".jpg");
			Bitmap bitmap = null;
			if (imageFile.exists()) {
				try {
					bitmap = BitmapFactory.decodeStream(new FileInputStream(
							imageFile));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			return bitmap;
		}
	}

不客气
fneg
fneg
回复 @zhaoshiyun0728 : 是不是没有在AndroidManifest中添加SD卡读写权限?
z
zhaoshiyun0728
为什么 代码运行到69行就不继续运行了呢 图片也保存不了
0
迷途d书童
迷途d书童

以图片文件形式保存

马小玲
你好 谢了 兄弟 能给个例子吗 我是还是个新手
0
fneg
fneg
class DownLoadItemImageAsync extends
			AsyncTask<TaobaokeItem, Integer, Bitmap> {

		private TaobaokeItem item;
	
		@Override
		protected Bitmap doInBackground(TaobaokeItem... params) {
			item = params[0];
			Bitmap bitmap = getBitmap();
			if (bitmap != null) {
				return bitmap;
			}
			return getItemPic(item.getPicUrl());
		}

		@Override
		protected void onPostExecute(Bitmap result) {
		
			Drawable drawable = new BitmapDrawable(result);
			setProgressBarIndeterminateVisibility(false);
			mImageSwitcher.setImageDrawable(drawable);

		}

		private Bitmap getItemPic(String picUrl) {
			URL url = null;
			URLConnection conn = null;
			InputStream in = null;
			Bitmap itemBitmap = null;
			try {
				url = new URL(picUrl);
				conn = url.openConnection();
				conn.setConnectTimeout(3 * 1000);
				conn.connect();

				in = conn.getInputStream();

				itemBitmap = BitmapFactory.decodeStream(in);
				storeInSD(itemBitmap);
			} catch (MalformedURLException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
				if (url != null) {
					url = null;
				}
				if (conn != null) {
					conn = null;
				}
				if (in != null) {
					try {
						in.close();
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
			return itemBitmap;
		}

		private void storeInSD(Bitmap bitmap) {
			File file = new File(FILE_DIR);
			if (!file.exists()) {
				file.mkdir();
			}
			File imageFile = new File(file, item.getNumIid() + ".jpg");
			try {
				imageFile.createNewFile();
				FileOutputStream fos = new FileOutputStream(imageFile);
				bitmap.compress(CompressFormat.JPEG, 50, fos);
				fos.flush();
				fos.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		private Bitmap getBitmap() {
			File imageFile = new File(FILE_DIR, item.getNumIid() + ".jpg");
			Bitmap bitmap = null;
			if (imageFile.exists()) {
				try {
					bitmap = BitmapFactory.decodeStream(new FileInputStream(
							imageFile));
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			return bitmap;
		}
	}

马里士多德
有没有Demo啊,求帮助啊啊
刘学炜
刘学炜
good good study ,day day up
马小玲
谢了 兄弟
0
goyier
goyier

也遇到同样问题,学习了

0
t
tanktu
参考 LruCache
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部