如何从Internet上保存内部存储下载的位图

Olk*_*afa 3 android

我正在使用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();
        }

    }
Run Code Online (Sandbox Code Playgroud)

任何帮助将不胜感激.:)

谢谢.

Joa*_*ist 15

您可以在内部存储上保存和加载图像,如下所示:保存:

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();
    }
}
Run Code Online (Sandbox Code Playgroud)

加载:

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; 
} 
Run Code Online (Sandbox Code Playgroud)

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