【问题标题】:Unable to store Image into internal storage directory无法将图像存储到内部存储目录
【发布时间】:2012-11-24 00:14:45
【问题描述】:

我想创建一个文件夹并将图像存储到手机的内部存储器中。我尝试使用下面的代码从 url 下载图像。它设法将图像加载到 imageView 中,但无法在内部存储中存储和创建文件夹。另外,我没有收到任何警告或错误消息。知道下面的代码有什么问题吗?

        Bitmap bm = null;
        InputStream in;

            try{

                in = new java.net.URL("http://blogs.computerworld.com/sites/computerworld.com/files/u177/google-nexus-4.jpg").openStream();
                bm = BitmapFactory.decodeStream(new PatchInputStream(in));
                File mydir = this.getDir("mydir", Context.MODE_PRIVATE);
                mydir.mkdirs();
                File fileWithinMyDir = new File(mydir, "myfile");
                FileOutputStream out = new FileOutputStream(fileWithinMyDir);
                bm.compress(Bitmap.CompressFormat.JPEG, 85, out);



            }
            catch(Exception e1){
                e1.printStackTrace();
            }
  ImageView img = (ImageView) findViewById(R.id.image_display);
  img.setImageBitmap(bm);

【问题讨论】:

  • 另一个问题是,如果我在内部存储中创建,它也会出现在画廊中吗?

标签: android directory storage store


【解决方案1】:

一旦你有你的位图bm

FileOutputStream fos;

try {
    fos = openFileOutput("file_Name", Context.MODE_PRIVATE);
    bm.compress(Bitmap.CompressFormat.PNG, 100, fos);
    fos.close();
}catch (Exception e){
...
}

将您的文件保存到内部存储中

【讨论】:

    【解决方案2】:

    使用以下内容:

    String path=Environment.getExternalStorageDirectory()
                                    .toString() + File.separator 
    

    获取目录并保存图片。

    【讨论】:

    • 我试过用这个。仍然无法正常工作。不知道为什么或如何。
    • 这也让我很沮丧。 getExternalStorageDirectory() 将返回类似 /storage/emulated/0/... 当在一些或只有没有外部存储的 Nexus 设备上没有该目录时。它应该是 /storage/emulated/legacy/...!为什么在我的 Nexus10 上它返回的路径不存在且不正确?呜呜……
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    • 2018-05-28
    • 2012-07-18
    • 2011-08-11
    • 2015-09-07
    相关资源
    最近更新 更多