【问题标题】:Saving image by url to internal storage通过 url 将图像保存到内部存储
【发布时间】:2016-08-06 04:53:31
【问题描述】:

请,需要帮助。我在将其保存到我的内部存储时遇到此错误 java.io.FileNotFoundException: http://news.yandex.ru/quotes/1507.png (可以通过浏览器看到)。 这是我的方法:

无效下载图(字符串链接){ 试试 {

                URL url = new URL(link);
                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.setDoOutput(true);
                urlConnection.connect();

                File dbDirectory = new File(mctx.getFilesDir().getAbsolutePath()+ File.separator+"yqimages/");
                if(!dbDirectory.exists())dbDirectory.mkdir();
                String fname=link.substring(link.lastIndexOf("/")+1,link.length());
                File file = new File(dbDirectory, fname);
                FileOutputStream fileOutput = new FileOutputStream(file);
                InputStream inputStream = urlConnection.getInputStream();

                byte[] buffer = new byte[1024];
                int bufferLength;

                while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                    fileOutput.write(buffer, 0, bufferLength);
                }
                fileOutput.close();
            } catch (final Exception e) {
                e.printStackTrace();
            }
}

你能写一个方法来下载\保存这个特定的图像(如上所示)吗?任何帮助表示赞赏! 得到它!!问题不在于代码,而在于图像https://news.yandex.ru/quotes/1507.png。由于某种原因,这张图片无法保存,而其他图片则无法保存。和“httpS://”有关系吗?

【问题讨论】:

  • 好的,你正在使用 android - 应该在标签中指定,不要浪费我们的时间

标签: java android image save fileoutputstream


【解决方案1】:

here 解释所有关于在 android 中下载和保存图像的信息。

并且不要忘记在 Manifest 中添加读写外部存储器文件的权限。

【讨论】:

  • 谢谢,已经找到解决方案,正在更改 urlConnection.setDoOutput(false);成功了。无论如何感谢!!!
【解决方案2】:
// ist put the permission in Manifest.xml hte permission are below
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
 
private void saveimage() {
    bitmap = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
    String time  = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(System.currentTimeMillis());
    File path  = Environment.getExternalStorageDirectory();
    File dir = new File(path+"/Gallery");
    dir.mkdir();
    String imagename = time+".PNG";
    File file = new File(dir,imagename);
    OutputStream out;
    try {
        out = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.PNG,100,out);
        out.flush();
        out.close();
        Toast.makeText(Show_Online.this, "Image Save To Gallery", 
Toast.LENGTH_SHORT).show();
    }
    catch (Exception e){
        Toast.makeText(Show_Online.this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
  

【讨论】:

  • 代码可以正常工作,但您应该添加一些文本来证明代码的合理性
猜你喜欢
  • 1970-01-01
  • 2020-05-30
  • 2018-02-20
  • 2013-10-28
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多