【问题标题】:Android: Issue in downloading to SDAndroid:下载到 SD 时出现问题
【发布时间】:2012-09-17 18:53:40
【问题描述】:

我开发了一个应用程序,用户可以在其中从服务器下载 .mp3 文件。并预定义了 mnt/sdcard/foldername 的路径以保存此类文件。我已经在 HTC、LG、三星运行了我的程序,但是当我在三星 Galaxy s2 上运行相同的程序时遇到了一个无法在 mnt/sdcard/foldername 中写入(存储)的问题并尝试了

 Environment.getExternalStorageDirectory().getAbsolutePath()

但它显示给定路径中的下载文件名和每个文件属性的零字节。有解决这个问题的想法吗?

【问题讨论】:

    标签: java android android-sdcard


    【解决方案1】:

    SG2 通常没有 sd 卡,而是使用内部闪存作为“外部”存储。我已经用这段代码解决了这个问题:

        private File initCacheDir() {
            String sdState = android.os.Environment.getExternalStorageState();
                File imageCacheDir;
                if (sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
                    File sdDir = android.os.Environment.getExternalStorageDirectory();      
                    imageCacheDir = new File(sdDir, "Android/data/" + App.PACKAGE_NAME + "/files/imageCache");
                }
                else
                    imageCacheDir = context.getCacheDir();
    
                if(!imageCacheDir.exists())
                     imageCacheDir.mkdirs();        
                return imageCacheDir;
    }
    

    请注意,此代码为您提供了缓存目录的位置,该目录通常位于 sd 卡上的 Android/data 文件夹中。

    您可以在此处找到有关如何使用 SG2 解决此问题的更多详细信息: How could i get the correct external storage on Samsung and all other devices?

    【讨论】:

    • 是的!谢谢你的回复,但我的意思是如果我使用 /data 路径永久存储在外部卡中,它不能存储更多数量的文件或大空间文件.. 也似乎是临时存储。谢谢!
    【解决方案2】:

    试试这个

     if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
            cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"yourfile");
        else
            cacheDir=context.getCacheDir();
        if(!cacheDir.exists())
            cacheDir.mkdirs();
    

    【讨论】:

    • 谢谢!还是要出问题。我的问题不是制作目录,必须下载到sdcard。我尝试使用相同的代码下载图像,它的工作。如果我去 mp3,它缺乏。仅三星 Galaxy s2 会出现此问题。正如@bjorncs 所说/它使用闪存来解决它。
    【解决方案3】:

    我终于找到了代码

    public void download(String urlToDownload){
    
    URLConnection urlConnection = null;
    
    try{
    
        URL url = new URL(urlToDownload);
    
        //Opening connection of currrent url
    
        urlConnection = url.openConnection();
        urlConnection.connect();
    
        //int lenghtOfFile = urlConnection.getContentLength();
    
    
    String PATH = Environment.getExternalStorageDirectory() + "/1/";
    
    File file = new File(PATH);
    file.mkdirs();
    File outputFile = new File(file, "file.mp3");
    FileOutputStream fos = new FileOutputStream(outputFile);
    
    InputStream is = url.openStream();
    
    
    byte[] buffer = new byte[1024];
    
    int len1 = 0;
    
    while ((len1 = is.read(buffer)) != -1) {
        fos.write(buffer, 0, len1);
    }
    
    fos.close();
    is.close();
    
    System.out.println("downloaded"+urlToDownload);
    
    }catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    
    }
    
    }
    

    来源:link

    【讨论】:

      猜你喜欢
      • 2015-09-14
      • 1970-01-01
      • 2011-03-25
      • 2017-08-12
      • 1970-01-01
      • 1970-01-01
      • 2011-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多