当前位置: 首页 > 知识库问答 >
问题:

无法解码流java.io.FileNotFoundExcgia /storage/emulated/0

彭硕
2023-03-14

你好,我试图保存图片从网址在我的应用程序,但当我试图访问内存放置数据,一个错误出来

无法解码流java.io.FileNotFoundException/storage/emulated/0打开失败:enoint(没有这样的文件或目录)

这是我的DownloadManager课程

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();



}

共有1个答案

许安邦
2023-03-14

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

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
 类似资料:
  • 我尝试使用以下方法从URI获取路径: 当我尝试压缩位图时: 我得到这个错误:

  • 你好,我试图保存在我的应用程序上拍摄的照片,但当我试图访问内存以放置数据时,会出现错误 无法解码流java.io.FileNotFoundException/storage/emulated/0打开失败:enoint(没有这样的文件或目录) 这是我的代码。

  • 可以帮助任何人回答这个(我认为很简单)问题吗?我是Android系统的纽比。谢谢你!

  • 问题内容: 嘿,我不确定为什么每次选择图库中的图像时都会出现这种情况吗? 这是代码: 错误: 问题答案: 不要假设有文件路径。Android 4.4及更高版本即将删除它们。而且您获得的uri已经没有路径。 您仍然可以通过()或文件描述符访问文件内容。 在这里进行了解释:ContentProviders:打开一个文档(向下滚动,指向该节的链接似乎已损坏) 而且确实适用于较旧的android版本。

  • 我知道这是一个老旧的,但我似乎没有找到一种方法来管理它... 我必须捕获一个相机图像,以供以后的IO处理,将其传输到一个网络共享,但在此之前我失败了很多...在阅读了一些文章之后,我找到了在KitKat上实际执行的方法,检查运行的SDK,并且它工作了…但是,现在我也在牛轧糖设备上测试它。 对于牛轧糖,我已经读到应该使用提供程序,并且我正在尝试... 捕获的映像必须存储在一个特定的文件夹中,该文件夹