如何保存从互联网上下载的内部存储位图

我正在使用Asynctask从互联网下载图像。

我想将这个图像保存到内部存储,然后我想使用这个图像。

我可以下载成功,但我不能找到它存储的内部存储路径。

这个DownloadImages.java

private class DownloadImages extends AsyncTask<String,Void,Bitmap> { private Bitmap DownloadImageBitmap(){ HttpURLConnection connection = null; InputStream is = null; try { URL get_url = new URL("http://www.medyasef.com/wp-content/themes/medyasef/images/altlogo.png"); connection = (HttpURLConnection) get_url.openConnection(); connection.setDoInput(true); connection.setDoOutput(true); connection.connect(); is = new BufferedInputStream(connection.getInputStream()); final Bitmap bitmap = BitmapFactory.decodeStream(is); // ?????????? } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { connection.disconnect(); try { is.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } @Override protected Bitmap doInBackground(String... params) { return DownloadImageBitmap(); } } 

任何帮助将不胜感激。 🙂

谢谢。

您可以像这样保存和加载内部存储器上的图像:保存:

 public static void saveFile(Context context, Bitmap b, String picName){ FileOutputStream fos; try { fos = context.openFileOutput(picName, Context.MODE_PRIVATE); b.compress(Bitmap.CompressFormat.PNG, 100, fos); } catch (FileNotFoundException e) { Log.d(TAG, "file not found"); e.printStackTrace(); } catch (IOException e) { Log.d(TAG, "io exception"); e.printStackTrace(); } finally { fos.close(); } } 

加载:

 public static Bitmap loadBitmap(Context context, String picName){ Bitmap b = null; FileInputStream fis; try { fis = context.openFileInput(picName); b = BitmapFactory.decodeStream(fis); } catch (FileNotFoundException e) { Log.d(TAG, "file not found"); e.printStackTrace(); } catch (IOException e) { Log.d(TAG, "io exception"); e.printStackTrace(); } finally { fis.close(); } return b; } 

但是,如果你想再次找到它,如果应用程序关闭,你将需要以某种方式保存imageName。 我会推荐一个将imageNames映射到数据库中的条目的SQLLite数据库。

我在Kotlin上的解决方案。 (根据另一个答案)

 import android.content.Context import android.graphics.Bitmap import android.graphics.BitmapFactory import java.io.FileInputStream import java.io.FileNotFoundException import java.io.FileOutputStream import java.io.IOException class InternalStorageProvider(var context: Context) { fun saveBitmap(bitmap: Bitmap, imageName: String) { var fileOutputStream: FileOutputStream? = null try { fileOutputStream = context.openFileOutput(imageName, Context.MODE_PRIVATE) bitmap.compress(Bitmap.CompressFormat.PNG, 100, fileOutputStream) } catch (e: FileNotFoundException) { e.printStackTrace() } catch (e: IOException) { e.printStackTrace() } finally { fileOutputStream?.close() } } fun loadBitmap(picName: String): Bitmap? { var bitmap: Bitmap? = null var fileInputStream: FileInputStream? = null try { fileInputStream = context.openFileInput(picName) bitmap = BitmapFactory.decodeStream(fileInputStream) } catch (e: FileNotFoundException) { e.printStackTrace() } catch (e: IOException) { e.printStackTrace() } finally { fileInputStream?.close() } return bitmap } } 

你有没有考虑过使用Glide库(或毕加索)来下载图片? 这样你就可以抽象所有关于Http连接,磁盘保存,内存和磁盘缓存,离线功能等的低级细节。另外,如果你选择Glide,你会自动在图片加载的动画中获得一些整洁的淡入淡出。

示例(kotlin)

要下载到磁盘:

 Glide.with(applicationContext) .load(user.picUrl) .downloadOnly(object : SimpleTarget<File>() { override fun onResourceReady(res: File, glideAnimation: GlideAnimation<in File>) {} }) 

从缓存中加载,或者如果缓存中不可用则下载:

 Glide.with(callingActivity.applicationContext) .load(wallPostViewHolder.mUser!!.picUrl) .skipMemoryCache(true) .diskCacheStrategy(DiskCacheStrategy.SOURCE) .into(wallPostViewHolder.vPic) 

尝试这个:

 String path = Environment.getExternalStorageDirectory().toString(); OutputStream fOut = null; String imageName = "yourImageName"; File file = new File(path, imageName); try { fOut = new FileOutputStream(file); if (!yourBitmap.compress(Bitmap.CompressFormat.JPEG, 90, fOut)) { Log.e("Log", "error while saving bitmap " + path + imageName); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); }