简体   繁体   English

如何从内部存储读取图像

[英]How to read an image from internal storage

I have downloaded an image to my app data/data/package memory. 我已将图像下载到我的应用程序数据/数据/包内存中。 it is downloaded, but problem is that i did not got path to display image in imageviewer. 它已下载,但问题是我没有在imageviewer中显示图像的路径。 but could not. 但不能。 please tell me how to display. 请告诉我如何显示。 Please attention on path. 请注意路径。 thank you Here is my download code. 谢谢这是我的下载代码。

ImageView imageView = (ImageView) findViewById(R.id.iv);
        String mUrl = "https://cometonice.com/im.gif";
        InputStreamVolleyRequest request = new InputStreamVolleyRequest(Request.Method.GET, mUrl,
                new Response.Listener<byte[]>() {
                    @Override
                    public void onResponse(byte[] response) {
                        // TODO handle the response
                        try {
                            if (response != null) {

                                FileOutputStream outputStream;
                                String name = "im.gif";
                                outputStream = openFileOutput(name, Context.MODE_PRIVATE);
                                outputStream.write(response);
                                outputStream.close();
                                Toast.makeText(MainActivity.this, "Download complete.", Toast.LENGTH_LONG).show();
                            }
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            Log.d("KEY_ERROR", "UNABLE TO DOWNLOAD FILE");
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                // TODO handle the error
                error.printStackTrace();
            }
        }, null);
        RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext(), new HurlStack());
        mRequestQueue.add(request);

Well you can use The ImageRequest of volley like this : 好吧,您可以像这样使用volley的ImageRequest

ImageRequest request = new ImageRequest(url,
    new Response.Listener<Bitmap>() {
        @Override
        public void onResponse(Bitmap bitmap) {
            imageView.setImageBitmap(bitmap);
            saveBitmapToFile(bitmap)
        }
    }, 0, 0, null,
    new Response.ErrorListener() {
        public void onErrorResponse(VolleyError error) {

        }
    });

and save it like : 并保存为:

public String saveBitmapToFile(Bitmap bitmap) {
        FileOutputStream out = null;
        String filename = null;
        try {
            File f = new File(Environment.getExternalStorageDirectory(), "myapp");
            if (!f.exists()) {
                f.mkdirs();
            }
            filename = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myapp/" + UUID.randomUUID().toString() + ".jpg";
            out = new FileOutputStream(filename);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 20, out);
            out.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return filename;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM