0

回答:

Needed the call to getExternalFilesDir(p); 像这样:

String p = thepathblah; 
File path=context.getExternalFilesDir(p);

编辑编辑:

虽然我知道它Environment.DIRECTORY_PICTURES正在返回,Pictures/但我认为这可行,因为在 android 中我假设文件指针已经指向您的应用程序空间(类似于 c# 中的排序)。所以在这个:

 String p = Environment.DIRECTORY_PICTURES + "/" + s.getClient().getFirstName()+s.getClient().getLastName() +
 "/" + s.getPackage().getName() +
 (mSession.getSessionDate().getMonth()+1)  +  
 mSession.getSessionDate().getDate() +  
  (mSession.getSessionDate().getYear()+1900);

我以为正在获取完整路径,实际上我正在为此编写一个文件,没有任何问题。事实证明,虽然要删除单个文件(并加载它们),但我需要一个更完整的路径,它最终是:

 String p = Environment.DIRECTORY_PICTURES + "/" + s.getClient().getFirstName()+s.getClient().getLastName() +
 "/" + s.getPackage().getName() +
 (mSession.getSessionDate().getMonth()+1)  +  
 mSession.getSessionDate().getDate() +  
  (mSession.getSessionDate().getYear()+1900);

 File dir = new File("/sdcard/Android/data/com.software.oursoftware/files/"+p);

不确定我是否可以认为上述链接对所有 Honeycomb 设备都有效,特别是/sdcard/Android/data/packagespace/files/

这是安全的使用它还是我必须为蜂窝设备做一些更动态的事情???

编辑:这是我的小测试功能代码,只需将内容写入文件夹......

   String p = Environment.DIRECTORY_PICTURES + "/" + s.getClient().getFirstName()+s.getClient().getLastName() + "/" + s.getPackage().getName() + (mSession.getSessionDate().getMonth()+1)  +  mSession.getSessionDate().getDate() + (mSession.getSessionDate().getYear()+1900);
            File path = mContext.getExternalFilesDir(p);
            File file = new File(path, "DemoPicture.jpg");

            try {
                // Very simple code to copy a picture from the application's
                // resource into the external file.  Note that this code does
                // no error checking, and assumes the picture is small (does not
                // try to copy it in chunks).  Note that if external storage is
                // not currently mounted this will silently fail.
                InputStream is = getResources().openRawResource(R.drawable.ic_contact_picture);
                OutputStream os = new FileOutputStream(file);
                byte[] data = new byte[is.available()];
                is.read(data);
                os.write(data);
                is.close();
                os.close();

                // Tell the media scanner about the new file so that it is
                // immediately available to the user.
                MediaScannerConnection.scanFile(mContext,
                        new String[] { file.toString() }, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                    public void onScanCompleted(String arg0, Uri arg1) {
                         Log.i("ExternalStorage", "Scanned " + arg0 + ":");
                            Log.i("ExternalStorage", "-> uri=" + arg1);

                    }
                });
            } catch (IOException e) {
                // Unable to create file, likely because external storage is
                // not currently mounted.
                Log.w("ExternalStorage", "Error writing " + file, e);
            }

然后我尝试删除此文件夹的方式:

String p = Environment.DIRECTORY_PICTURES + "/" + firstName+lastName +"/" + pName+pDate;
File dir=new File(p); 
deleteRecursive(dir);

结果是

Pictures/ShaneThomas/Portrait882011/

哪个可以写一个文件,测试过,但如果我试着说:

void deleteRecursive(File dir)
{
    Log.d("DeleteRecursive", "DELETEPREVIOUS TOP" + dir.getPath());
    if (dir.isDirectory())
    {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) 
        {
           File temp =  new File(dir, children[i]);
           if(temp.isDirectory())
           {
               Log.d("DeleteRecursive", "Recursive Call" + temp.getPath());
               deleteRecursive(temp);
           }
           else
           {
               Log.d("DeleteRecursive", "Delete File" + temp.getPath());
               boolean b = temp.delete();
               if(b == false)
               {
                   Log.d("DeleteRecursive", "DELETE FAIL");
               }
           }
        }

        dir.delete();
    }
}

dir.isDirectory 总是假的!?我从堆栈溢出中得到了这个删除文件/目录代码,但对它为什么不起作用感到困惑?

我确实有这套:

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

3 回答 3

2

File.isDirectory()返回 false有几个原因:

  1. 路径指向文件(显然),而不是目录。
  2. 路径无效(即不存在这样的文件/目录)。
  3. 您的应用程序没有足够的权限来确定路径是否指向目录。

通常,如果isDirectory()返回 true,则您有指向目录的路径。但是如果isDirectory()返回 false,那么它可能是也可能不是目录。

在您的特定情况下,该路径很可能不存在。您需要调用dir.mkdirs()以创建路径中的所有目录。dir.mkdirs()但是由于您只需要递归地删除它们,因此在此之后调用仅删除该目录是没有意义的。

于 2011-08-08T20:06:19.453 回答
1

我想你想添加

dir.mkdirs()紧随其后File dir=new File(p)。mkdirs() 是负责实际创建目录的方法,而不是 new File()。

于 2011-08-08T20:01:27.747 回答
1

好的,它已经回答了,但有时由于示例 reasone:permission 会引发问题:

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

如果你忘记了这个权限,你总是会得到错误的结果。

于 2015-09-05T19:37:56.887 回答