您好,我试图将从url获取的图片保存到应用程序中,但是当我试图访问内存以放置数据时,会出现一个错误
无法解码流java.io.fileNotFoundException/storage/emulated/0 open失败:enoent(没有这样的文件或目录)
这是我的下载管理类

public static ArrayList<String>  urls = new ArrayList<String>();

public static OnDownloadCompleteListener downloadCompleteListener;


public static void copyFile(String sourceFile, String destinationFile) {
    FileInputStream inputStream = null;
    FileOutputStream outputStream = null;
    try {
        inputStream = new FileInputStream(sourceFile);
        outputStream = new FileOutputStream(destinationFile);

        byte[] buffer = new byte[G.DOWNLOAD_BUFFER_SIZE];
        int len;
        while ((len = inputStream.read(buffer)) > 0) {
            outputStream.write(buffer, 0, len);
        }
    }
    catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (inputStream != null) {
            try {
                inputStream.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (outputStream != null) {
            try {
                outputStream.flush();
                outputStream.close();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


public static void initialize() {
    downloadCompleteListener = new OnDownloadCompleteListener() {

        @Override
        public void onDownloadComplete(String url, String localPath) {
            Log.i("LOG", "Image Download Complete, Original URL: " + url + ", Save Path: " + localPath);
            String newPath = localPath.replace("/temp/", "/final/");
            copyFile(localPath, newPath);
            String filename = HelperString.getFileName(localPath);
            new File(localPath).delete();

            Set<ImageView> imageViews = AdapterSerials.imageMap.keySet();
            for (ImageView imageView: imageViews) {
                if (AdapterSerials.imageMap.get(imageView).equals(filename)) {
                    if (imageView != null) {
                        BitmapFactory.Options options = new BitmapFactory.Options();
                        //options.inSampleSize = 8;
                        Bitmap bitmap = BitmapFactory.decodeFile(newPath, options);
                        imageView.setImageBitmap(bitmap);
                    }
                }
            }
        }
    };
}


public static void addToDownloadList(String url, ImageView imgLogo) {
    String filename = HelperString.getFileName(url);
    AdapterSerials.imageMap.put(imgLogo, filename);

    if (urls.contains(url)) {
        return;
    }

    if (new File(G.DIR_FINAL + "/" + filename).exists()) {
        return;
    }

    urls.add(url);


    DownloadRequest downloadRequest = new DownloadRequest()
            .downloadPath("http://87.236.215.180/mazi/" + url)
            .filepath(G.DIR_TEMP + "/" + filename)
            .listener(downloadCompleteListener)
            .download();
}

这是我的下载请求类:
public class DownloadRequest {

private int downloadedSize;
private int totalSize;
private int percent;


public int getDownloadedSize() {
    return downloadedSize;
}


public int getTotalSize() {
    return totalSize;
}


public int getPercent() {
    return percent;
}


public DownloadRequest download() {
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                URL url = new URL(downloadPath);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("GET");
                connection.setDoOutput(true);
                connection.connect();

                totalSize = connection.getContentLength();

                File file = new File(filepath);
                if (file.exists()) {
                    file.delete();
                }

                FileOutputStream outputStream = new FileOutputStream(filepath);

                InputStream inputStream = connection.getInputStream();
                byte[] buffer = new byte[G.DOWNLOAD_BUFFER_SIZE];
                int len = 0;
                while ((len = inputStream.read(buffer)) > 0) {
                    outputStream.write(buffer, 0, len);
                    downloadedSize += len;

                    percent = (int) (100.0f * (float) downloadedSize / totalSize);
                    if (percent == 100 && listener != null) {

                        G.HANDLER.post(new Runnable() {

                            @Override
                            public void run() {
                                listener.onDownloadComplete(downloadPath, filepath);
                            }
                        });
                    }

                    if (simulate) {
                        try {
                            Thread.sleep(100);
                        }
                        catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }

                outputStream.close();
            }
            catch (MalformedURLException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
        }
    });

    thread.start();

    return this;
}

private String                     downloadPath;
private String                     filepath;
private OnDownloadCompleteListener listener;
private boolean                    simulate;


public DownloadRequest downloadPath(String value) {
    downloadPath = value;
    return this;
}


public DownloadRequest filepath(String value) {
    filepath = value;
    return this;
}


public DownloadRequest listener(OnDownloadCompleteListener value) {
    listener = value;
    return this;
}


public DownloadRequest simulate(boolean value) {
    simulate = value;
    return this;
}

这是我的G班:
public class G extends Application {

public static Context                  context;
public static final String             SDCARD               = Environment.getExternalStorageDirectory().getAbsolutePath();
public static final String             DIR_APP              = SDCARD + "/serial";
public static final String             DIR_CACHE            = DIR_APP + "/cache";
public static LayoutInflater           inflater;
public static final Handler            HANDLER              = new Handler();
public static Activity                 currentActivity;

public static StructSerials            selectedSerials;
public static StructFavSerials         selectedFavSerials;
public static ArrayList<StructComment> rates                = new ArrayList<StructComment>();

public static final int                DOWNLOAD_BUFFER_SIZE = 8 * 1024;
public static final String             DIR_FINAL            = DIR_APP + "/final";
public static final String             DIR_TEMP             = DIR_APP + "/temp";
public static String                   android_id;
public static SharedPreferences        preferences;


@Override
public void onCreate() {
    super.onCreate();

    context = getApplicationContext();
    preferences = PreferenceManager.getDefaultSharedPreferences(context);
    initImageLoader(getApplicationContext());

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    android_id = Secure.getString(context.getContentResolver(),
            Secure.ANDROID_ID);
    new File(DIR_APP).mkdirs();
    new File(DIR_CACHE).mkdirs();
    new File(DIR_TEMP).mkdirs();
    new File(DIR_FINAL).mkdirs();



    DownloadManager.initialize();



}

最佳答案

记住将此权限添加到您的AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

08-17 20:29