Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.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 为什么只有1/10的图像加载并保存在sd卡中_Android_Android Sdcard_Android Image - Fatal编程技术网

Android 为什么只有1/10的图像加载并保存在sd卡中

Android 为什么只有1/10的图像加载并保存在sd卡中,android,android-sdcard,android-image,Android,Android Sdcard,Android Image,我正在从存储在数组中的URL加载10个图像。只有最后一个索引url正在SD卡中加载和保存图像。这是我的密码: for(int j=0; j<List.size();j++) { reviewImageLink =List.get(j).get(TAG_Image).toString(); URL reviewImageURL; String name = reviewImageLink;//.s

我正在从存储在数组中的URL加载10个图像。只有最后一个索引url正在SD卡中加载和保存图像。这是我的密码:

  for(int j=0; j<List.size();j++)
        {
             reviewImageLink =List.get(j).get(TAG_Image).toString();
             URL reviewImageURL;
            String name = reviewImageLink;//.substring(reviewImageLink .lastIndexOf("/") + 1,reviewImageLink.length());
                try {
                    reviewImageURL = new URL(reviewImageLink);
                    if (!hasExternalStoragePublicPicture(name)) {
                        isImage = false;
                        new DownloadImageTask().execute(reviewImageURL);
                        Log.v("log_tag", "if");
                        isImage = true;
                        File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.directory));
                    //if(!sdImageMainDirectory.exists()){
                        sdImageMainDirectory.mkdirs();
                        File file = new File(sdImageMainDirectory, name);
                        Log.v("log_tag", "Directory created");}
            }
                catch (MalformedURLException e) {
                    Log.v(TAG, e.toString());   }
        }

    }//try 
    catch (Exception e) {
            e.printStackTrace();}

DownloaderTask:

class DownloadImageTask extends AsyncTask<URL, Integer, Bitmap> {
    // This class definition states that DownloadImageTask will take String
    // parameters, publish Integer progress updates, and return a Bitmap
    protected Bitmap doInBackground(URL... paths) {
        URL url;
        try {
            url = paths[0];
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            int length = connection.getContentLength();
            InputStream is = (InputStream) url.getContent();
            byte[] imageData = new byte[length];
            int buffersize = (int) Math.ceil(length / (double) 100);
            int downloaded = 0;
            int read;
            while (downloaded < length) {
                if (length < buffersize) {
                    read = is.read(imageData, downloaded, length);
                } else if ((length - downloaded) <= buffersize) {
                    read = is.read(imageData, downloaded, length- downloaded);
                } else {
                    read = is.read(imageData, downloaded, buffersize);
                }
                downloaded += read;
                publishProgress((downloaded * 100) / length);
            }
            Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0,length);
            if (bitmap != null) {
                Log.i(TAG, "Bitmap created");
            } else {
                Log.i(TAG, "Bitmap not created");
            }
            is.close();
            return bitmap;
        } catch (MalformedURLException e) {
            Log.e(TAG, "Malformed exception: " + e.toString());
        } catch (IOException e) {
            Log.e(TAG, "IOException: " + e.toString());
        } catch (Exception e) {
            Log.e(TAG, "Exception: " + e.toString());
        }
        return null;

    }

    protected void onPostExecute(Bitmap result) {
        String name = reviewImageLink.substring(reviewImageLink.lastIndexOf("/") + 1,reviewImageLink.length());
        if (result != null) {
            hasExternalStoragePublicPicture(name);
            saveToSDCard(result, name);
            isImage = true;

        } else {
            isImage = false;

        }
    }
}

public void saveToSDCard(Bitmap bitmap, String name) {
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED.equals(state)) {
        mExternalStorageAvailable = mExternalStorageWriteable = true;
        Log.v(TAG, "SD Card is available for read and write "+ mExternalStorageAvailable + mExternalStorageWriteable);
        saveFile(bitmap, name);
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
        Log.v(TAG, "SD Card is available for read "+ mExternalStorageAvailable);
    } else {
        mExternalStorageAvailable = mExternalStorageWriteable = false;
        Log.v(TAG, "Please insert a SD Card to save your Video "+ mExternalStorageAvailable + mExternalStorageWriteable);
    }
}

private void saveFile(Bitmap bitmap, String name) 
{
    String filename = name;
    ContentValues values = new ContentValues();
    File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.directory));
    sdImageMainDirectory.mkdirs();
    File outputFile = new File(sdImageMainDirectory, filename);
    values.put(MediaStore.MediaColumns.DATA, outputFile.toString());
    values.put(MediaStore.MediaColumns.TITLE, filename);
    values.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis());
    values.put(MediaStore.MediaColumns.MIME_TYPE, "output.jpeg");
    Uri uri = this.getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
    //Uri result = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, image);
    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

    try 
    {
        OutputStream outStream = this.getContentResolver().openOutputStream(uri);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 95, outStream);
        outStream.flush();
        outStream.close();
    }
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    } catch (IOException e) 
    {
        e.printStackTrace();
    }
}

private boolean hasExternalStoragePublicPicture(String name) {
    File sdImageMainDirectory = new File(Environment.getExternalStorageDirectory(), getResources().getString(R.string.directory));
    File file = new File(sdImageMainDirectory, name);
    if (file != null) 
    {
        file.delete();
    }
    return file.exists();
}

for(int j=0;j将列表传递给
DownloadImageTask
而不是
URL
,然后在
doInBackground
中放置一个for循环,

u需要在doInBackground中移动循环,而不是在循环中启动异步任务