在 Android 平板电脑 (3.1) 上向内存写入和读取图像时出现问题

发布于 2024-12-18 12:22:58 字数 2067 浏览 8 评论 0原文

我认为这不会太难,但在过去的几个小时里我一直在用头撞桌子,非常感谢一些帮助。本质上,我想从 url 获取图像,将其保存到内部存储器(不是 SD 卡),并能够检索该图像并稍后使用 ImageView 显示它。

这就是我从网址获取图片并将其写入内存的方式(网址存储在“图片”中):

                    String urlstring = pics[l][w];
                if (urlstring != null){
                    try {
                        URL url = new URL(urlstring);
                        InputStream input = url.openStream();
                        FileOutputStream output = openFileOutput(("specimage"+l) + ("" +w+".jpg"), MODE_PRIVATE);


                        byte[] buffer = new byte[input.available()];
                        int n = input.read(buffer, 0, buffer.length);
                        while (n >= 0) {
                            output.write(buffer, 0, buffer.length);
                            n = input.read(buffer, 0, buffer.length);
                        } 
                        output.close();
                        input.close();
                    } catch (Exception e) {
                        GlobalState.popupMessage(homePage, "Error", "Files could not be stored on disk");
                    }
                }

这就是我尝试检索它们的方式(路径是文件名):

private Bitmap getPic(String path){
     FileInputStream in;
     Bitmap bMap = null;
        BufferedInputStream buf;
        try {
            in = openFileInput(path);
            buf = new BufferedInputStream(in);
            byte[] bMapArray= new byte[buf.available()];
            buf.read(bMapArray);
            bMap = BitmapFactory.decodeStream(buf);
            if (in != null) {
            in.close();
            }
            if (buf != null) {
            buf.close();
            }
        } catch (Exception e) {
            System.out.println("excep.");
        }
        if (bMap == null) System.out.println("null");
    return bMap;
}

如果我这样做,图片不会显示,但程序不会崩溃。不会触发异常。然而,bMap 的值被指定为 null。我还在日志中收到此奇怪的消息:

DEBUG/skia(19358): --- SkImageDecoder::Factory returned null

请让我知道我做错了什么。我绞尽脑汁也没有结果。 我应该提到我在 ui 线程中做了 setImageBitmap 。

I thought this wouldn't be too hard but have been banging my head against a desk for the last few hours and would really appreciate some help. Essentially, I want to get an image from a url, save it to internal memory (not sd card) and be able to retrieve that image and show it using an ImageView at a later time.

This is how I get the pictures from a url and write them to memory(urls are stored in "pics"):

                    String urlstring = pics[l][w];
                if (urlstring != null){
                    try {
                        URL url = new URL(urlstring);
                        InputStream input = url.openStream();
                        FileOutputStream output = openFileOutput(("specimage"+l) + ("" +w+".jpg"), MODE_PRIVATE);


                        byte[] buffer = new byte[input.available()];
                        int n = input.read(buffer, 0, buffer.length);
                        while (n >= 0) {
                            output.write(buffer, 0, buffer.length);
                            n = input.read(buffer, 0, buffer.length);
                        } 
                        output.close();
                        input.close();
                    } catch (Exception e) {
                        GlobalState.popupMessage(homePage, "Error", "Files could not be stored on disk");
                    }
                }

This is how I attempt to retrieve them (path is the filename):

private Bitmap getPic(String path){
     FileInputStream in;
     Bitmap bMap = null;
        BufferedInputStream buf;
        try {
            in = openFileInput(path);
            buf = new BufferedInputStream(in);
            byte[] bMapArray= new byte[buf.available()];
            buf.read(bMapArray);
            bMap = BitmapFactory.decodeStream(buf);
            if (in != null) {
            in.close();
            }
            if (buf != null) {
            buf.close();
            }
        } catch (Exception e) {
            System.out.println("excep.");
        }
        if (bMap == null) System.out.println("null");
    return bMap;
}

If I do this the picture does not show up, but the program does not crash. An exception is not triggered. However, the value of bMap is given as null. I also get this strange message in the log:

DEBUG/skia(19358): --- SkImageDecoder::Factory returned null

Please let me know what I'm doing wrong. I have been ransacking my brain to no avail.
I should mention I do setImageBitmap in the ui thread.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

穿透光 2024-12-25 12:22:58

试试这个,(替换为你的)然后让我知道会发生什么,

ImageView  img = (ImageView)findViewById(R.id.imgView1);
FileInputStream in;
     Bitmap bMap = null;
        BufferedInputStream buf;
        try {
            in = openFileInput("icon.png");
            buf = new BufferedInputStream(in);
            byte[] bMapArray= new byte[buf.available()];
            buf.read(bMapArray);
            bMap = BitmapFactory.decodeByteArray(bMapArray,0,bMapArray.length);
            img.setImageBitmap(bMap);
            if (in != null) {
            in.close();
            }
            if (buf != null) {
            buf.close();
            }
        } catch (Exception e) {
            System.out.println("excep.");
        }

Just try this, (Replace with your) and let me know what happen,

ImageView  img = (ImageView)findViewById(R.id.imgView1);
FileInputStream in;
     Bitmap bMap = null;
        BufferedInputStream buf;
        try {
            in = openFileInput("icon.png");
            buf = new BufferedInputStream(in);
            byte[] bMapArray= new byte[buf.available()];
            buf.read(bMapArray);
            bMap = BitmapFactory.decodeByteArray(bMapArray,0,bMapArray.length);
            img.setImageBitmap(bMap);
            if (in != null) {
            in.close();
            }
            if (buf != null) {
            buf.close();
            }
        } catch (Exception e) {
            System.out.println("excep.");
        }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文