将图像从字符串数组保存到SD卡和电话库

时间:2015-05-04 13:29:03

标签: android arrays

我正在使用字符串数组来保存图像,我从URL中获取图像。我有一个imageview,当swipped改变其中的图像。现在我想在SD卡上下载并保存我想要的任何图像,并希望它出现在新文件夹的手机库中。

我正在使用以下代码,但它无法正常工作,它根本没有显示任何错误。

private class ImageDownloadAndSave extends AsyncTask<String, Void, Bitmap> {
        @Override
        protected Bitmap doInBackground(String... arg0) {
            downloadImagesToSdCard("", "");
            return null;
        }

        private void downloadImagesToSdCard(String downloadUrl, String imageName) {
            try {
                URL url = new URL(thumb[j]);
                /* making a directory in sdcard */
                String sdCard = Environment.getExternalStorageDirectory()
                        .toString();
                File myDir = new File(sdCard, "test.jpg");

                /* if specified not exist create new */
                if (!myDir.exists()) {
                    myDir.mkdir();
                    Log.v("", "inside mkdir");
                }

                /* checks the file and if it already exist delete */
                String fname = imageName;
                File file = new File(myDir, fname);
                if (file.exists())
                    file.delete();

                /* Open a connection */
                URLConnection ucon = url.openConnection();
                InputStream inputStream = null;
                HttpURLConnection httpConn = (HttpURLConnection) ucon;
                httpConn.setRequestMethod("GET");
                httpConn.connect();

                if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    inputStream = httpConn.getInputStream();
                }

                FileOutputStream fos = new FileOutputStream(file);
                int totalSize = httpConn.getContentLength();
                int downloadedSize = 0;
                byte[] buffer = new byte[1024];
                int bufferLength = 0;
                while ((bufferLength = inputStream.read(buffer)) > 0) {
                    fos.write(buffer, 0, bufferLength);
                    downloadedSize += bufferLength;
                    Log.i("Progress:", "downloadedSize:" + downloadedSize
                            + "totalSize:" + totalSize);
                }

                fos.close();
                Log.d("test", "Image Saved in sdcard..");
            } catch (IOException io) {
                io.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

我正在通过

执行它
new ImageDownloadAndSave().execute("");

我的字符串数组的名称是“thumb”,它包含所有的URL。我做错了什么?

2 个答案:

答案 0 :(得分:0)

您应该在保存后将图像添加到图库,然后进行扫描。请检查以下主题:

Image, saved to sdcard, doesn't appear in Android's Gallery app

MediaScannerConnection

答案 1 :(得分:0)

该文件需要添加到Android MediaStore。这可以通过使用以下功能轻松完成。

public void scanFile(final File file) {
    try {
        new MediaScannerConnectionClient() {
            private MediaScannerConnection mMs;

            public void init() {
                mMs = new MediaScannerConnection(myContext, this);
                mMs.connect();
            }

            @Override
            public void onMediaScannerConnected() {
                mMs.scanFile(file.getAbsolutePath(), null); 
                mMs.disconnect();
            }

            @Override
            public void onScanCompleted(String path, Uri uri) {
            }

        }.init();
    } catch(Exception e) {
        // Device does not support adding files manually. 
        // Sending Broadcast to start MediaScanner (slower than adding manually)
        try {   
            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                    Uri.parse("file://" + Environment.getExternalStorageDirectory())));
        } catch(Exception ee) {
            // Something went terribly wrong. 
            // Can't add file to MediaStore and can't send Broadcast to start MediaScanner.
        }
    }
}

您只需在代码中添加一行:

// .....

fos.close();
Log.d("test", "Image Saved in sdcard..");

scanFile(file); // <------------- add this