Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将图像从字符串阵列保存到SD卡和手机图库_Android_Arrays - Fatal编程技术网

Android 将图像从字符串阵列保存到SD卡和手机图库

Android 将图像从字符串阵列保存到SD卡和手机图库,android,arrays,Android,Arrays,我使用字符串数组来保存图像,我从URL中获取IMAE。我有一个imageview,当切换时会更改其中的图像。现在,我想下载并保存我想要的SD卡上的任何图像,并希望它出现在手机图库中的一个新文件夹中 我正在使用下面的代码,但它不工作,它没有显示任何错误 private class ImageDownloadAndSave extends AsyncTask<String, Void, Bitmap> { @Override protected Bitma

我使用字符串数组来保存图像,我从URL中获取IMAE。我有一个imageview,当切换时会更改其中的图像。现在,我想下载并保存我想要的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();
            }
        }
    }

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

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


该文件需要添加到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
/。。。。。
fos.close();
Log.d(“测试”,“保存在SD卡中的图像…”);

扫描文件(文件);//您需要将文件添加到
MediaStore
。看看我的答案。@Byte仓鼠应该把它放在一个方法中,或者什么,我应该如何初始化和执行它?当使用文件管理器(例如“Astro”)时,你能看到文件吗?然后你只需要执行我发布的代码,用
文件
替换
扫描文件
。我会把它作为一个答案发布,等等。。。
// .....

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

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