【问题标题】:How to get the URI to a specified location?如何获取到指定位置的 URI?
【发布时间】:2012-09-07 11:43:29
【问题描述】:

我需要您的帮助来解决我在应用中发现的一个小问题。

在我的应用程序中,我使用此代码将资产文件夹的所有内容复制到我自己创建的文件夹中。

File wallpaperDirectory = new File("/sdcard/Wallpaper/");
    wallpaperDirectory.mkdirs();

    AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("");
    } catch (IOException e) {
        Log.e("tag", e.getMessage());
    }
    for(String filename : files) {
        InputStream in = null;
        OutputStream out = null;
        try {
          in = assetManager.open(filename);
          out = new FileOutputStream("/sdcard/Wallpaper/" + filename);
          copyFile(in, out);
          in.close();
          in = null;
          out.flush();
          out.close();
          out = null;
        } catch(Exception e) {
            Log.e("tag", e.getMessage());
        }       
}

private void copyFile(InputStream in, OutputStream out) throws IOException {
    byte[] buffer = new byte[1024];
    int read;
    while((read = in.read(buffer)) != -1){
      out.write(buffer, 0, read);
    }
}

现在我需要我创建的这个文件夹 (/sdcard/Wallpaper/) 的 URI,你知道吗?非常感谢

【问题讨论】:

    标签: android android-layout android-intent android-widget


    【解决方案1】:

    获取 SD 卡上特定文件夹的 Uri。

    String sdcard = Environment.getExternalStorageDirectory().getAbsolutePath();
    
    Uri.fromFile(new File(sdcard+"/Wallpaper/"));
    

    或者

    Uri.parse(sdcard+"/Wallpaper/")
    

    【讨论】:

    • 我需要文件夹的 'uri,而不是其中包含的特定文件。因为,根据我的想法,那么我必须随机获取此路径中的文件,可能使用光标
    【解决方案2】:
    Uri uri = Uri.fromFile(new File("/sdcard/Wallpaper/"));
    

    Uri uri = Uri.parse("/sdcard/Wallpaper/");
    

    【讨论】:

      【解决方案3】:

      试试这个

      获取文件路径

      File path = "/sdcard/Wallpaper/";
      

      通过以下代码将文件路径转换为Uri

      Uri imageurl = Uri.fromFile(path); 
      

      希望对你有帮助。

      【讨论】:

        【解决方案4】:

        您可以使用android.net.Uri 中的public static Uri fromFile (File file) 方法。

        【讨论】:

          猜你喜欢
          • 2016-06-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-09-25
          • 2011-04-22
          • 2018-01-04
          • 2010-11-18
          • 2017-03-21
          相关资源
          最近更新 更多