Android - 外部存储问题

时间:2016-10-11 18:36:48

标签: android android-external-storage

我有一个用android编写的小应用程序。

使用AsyncTask我尝试从服务器下载图像并将其存储在手机的外部或内部存储上。

图像格式为jpg,png等。

@Override
protected String doInBackground(String... param)
{
    try
    {
        for(int i = 0; i < param.length; i++)
        {
            String src = "http://myLINK/images/"+param[i];
            URL url = new URL(src);

            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.connect();

            InputStream input = connection.getInputStream();
            images.add(BitmapFactory.decodeStream(input));

            if (images.get(i) != null){
                saveImageToExternalStorage(images.get(i), param[i]);
            }
        }
    }
    catch (IOException io_exception){
        Log.e("ERROR", io_exception.getMessage());
    }

    return "success";
}



// save image to external storage
private void saveImageToExternalStorage(Bitmap image, String image_name)
{
    try
    {
        String state = Environment.getExternalStorageState();
        String path;

        // check if external storage exists
        if (Environment.MEDIA_MOUNTED.equals(state))
            path = Environment.getExternalStorageDirectory().toString();
        else
            path = Environment.getDataDirectory().toString();

        OutputStream fOut;

        File file = new File(path, "myDIR/images/"+image_name);
        fOut = new FileOutputStream(file);

        image.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        fOut.flush();
        fOut.close();
    }
    catch(Exception e){
        Log.e("ERROR", e.getMessage());
    }
}

错误是:

/storage/emulated/0/myDIR/images/57d7fa462860b_image1.jpeg: open failed: ENOENT (No such file or directory)

任何人都可以帮我吗?

0 个答案:

没有答案