无法在SdCard,Android中创建文件夹

时间:2012-04-29 21:14:13

标签: android android-sdcard mkdir fileoutputstream

我的代码曾经工作,但现在我甚至无法创建目录。 (请注意,“1”不会打印到logcat。)我不知道我打破了什么。我想在我创建的文件夹中保存图像,这是我的代码:

    String storedir = "/sdcard";
    String separator = "/";
    String mDateTime = formatter.format(cal.getTime());
    File newdir1 = new File(storedir + "/" + mDateTime);
            if (newdir1.mkdir())
                System.out.println("1");

    File newdir2 = new File(storedir + "/" + mDateTime + "/");
            if (newdir2.mkdir())
                System.out.println("2");

   // Fall into the catch because of ;
                if (fileNameLower.indexOf(".jpeg") > 0)
            fileExt = ".jpeg";
                String rand_fileName = Long.toString(System.currentTimeMillis());// +fileExt;
        String last_fileName = rand_fileName + fileExt;
            File storefile = new File(storedir + "/" + mDateTime + "/" + separator
                + last_fileName);
        String picAddress = storedir + "/" + mDateTime + "/" + separator
                + rand_fileName + ".png";
        System.out.println("135 : [" + storefile.toString() + "]/n");

        BufferedOutputStream bos = null;
        BufferedInputStream bis = null;
        try {
            Log.w("Process : " , "Starting to download process.");
            bos = new BufferedOutputStream(new FileOutputStream(storefile));
            bis = new BufferedInputStream(in);
            int c;
            while ((c = bis.read()) != -1) {
                bos.write(c);
                bos.flush();
            }
            Log.w("Process : " , "The image is downloaded.");
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new Exception("151 : !");
        } finally 
        {
            bos.close();
            bis.close();
        }



04-29 21:29:20.648: W/The image's type(5733): .jpeg
04-29 21:29:20.648: I/System.out(5733): log : [.jpeg]/n
04-29 21:29:20.652: I/System.out(5733): log : [/sdcard/2012-04-29/1335734960650.jpeg]/n
04-29 21:29:20.652: W/Process :(5733): Starting to download process.
04-29 21:29:20.652: W/System.err(5733): java.io.FileNotFoundException: /sdcard/2012-04-29/1335734960650.jpeg (Permission denied)
04-29 21:29:20.672: W/System.err(5733):     at org.apache.harmony.luni.platform.OSFileSystem.open(Native Method)
04-29 21:29:20.672: W/System.err(5733):     at dalvik.system.BlockGuard$WrappedFileSystem.open(BlockGuard.java:232)
04-29 21:29:20.682: W/System.err(5733):     at java.io.FileOutputStream.<init>(FileOutputStream.java:94)
04-29 21:29:20.682: W/System.err(5733):     at java.io.FileOutputStream.<init>(FileOutputStream.java:66)
04-29 21:29:20.682: W/System.err(5733):     at com.mobil.eposta.Baglanti.saveFile(Baglanti.java:345)
04-29 21:29:20.692: W/System.err(5733):     at com.mobil.eposta.Baglanti.saveAttachMent(Baglanti.java:255)
04-29 21:29:20.692: W/System.err(5733):     at com.mobil.eposta.Baglanti.EkiKaydet(Baglanti.java:235)
04-29 21:29:20.692: W/System.err(5733):     at com.mobil.eposta.Baglanti.Position(Baglanti.java:224)
04-29 21:29:20.692: W/System.err(5733):     at com.mobil.eposta.GoruntuleActivity$1.onClick(GoruntuleActivity.java:75)
04-29 21:29:20.707: W/System.err(5733):     at android.view.View.performClick(View.java:2485)
04-29 21:29:20.707: W/System.err(5733):     at android.view.View$PerformClick.run(View.java:9080)
04-29 21:29:20.712: W/System.err(5733):     at android.os.Handler.handleCallback(Handler.java:587)
04-29 21:29:20.712: W/System.err(5733):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-29 21:29:20.712: W/System.err(5733):     at android.os.Looper.loop(Looper.java:123)
04-29 21:29:20.712: W/System.err(5733):     at android.app.ActivityThread.main(ActivityThread.java:3683)
04-29 21:29:20.712: W/System.err(5733):     at java.lang.reflect.Method.invokeNative(Native Method)
04-29 21:29:20.712: W/System.err(5733):     at java.lang.reflect.Method.invoke(Method.java:507)
04-29 21:29:20.722: W/System.err(5733):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-29 21:29:20.732: W/System.err(5733):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-29 21:29:20.732: W/System.err(5733):     at dalvik.system.NativeStart.main(Native Method)

我应该怎样做sdcard的挂载情况?

在此代码中,结果为“NOT GOOD”语句。我该如何改变这种情况?

private static boolean  checkExternalMedia()
{
    boolean mExternalStorageAvailable = false;
    boolean mExternalStorageWriteable = false;
    String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {
        // We can read and write the media
        mExternalStorageAvailable = mExternalStorageWriteable = true;
    } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
        // We can only read the media
        mExternalStorageAvailable = true;
        mExternalStorageWriteable = false;
    } else {
        // Something else is wrong. It may be one of many other states,
        //  to know is we can neither read nor write
        Log.i("TAG","State="+state+" Not good");
        mExternalStorageAvailable = mExternalStorageWriteable = false;
    }

    Log.i("TAG","Available="+mExternalStorageAvailable+"Writeable="+mExternalStorageWriteable+" State"+state);
    return (mExternalStorageAvailable && mExternalStorageWriteable);
}

3 个答案:

答案 0 :(得分:1)

你需要

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在您的清单文件中

。请参阅有关stackoverflow的其他问题,例如Permission to write to the SD card

答案 1 :(得分:1)

  • 使用Environment.getExternalStorageDirectory().getAbsolutePath()
  • 而不是使用“/ sdcard”常量字符串

这将为您提供SD卡根路径(在某些设备中可能不仅仅是“/ sdcard”

  • 通常是“/ mnt / sdcard”,而不是“/ sdcard”。如果你想尝试一些事情,首先要做的就是直截了当。

  • 您初始化了一个“分隔符”常量,但您继续使用“/”来创建路径。这是从您粘贴的问题代码中引用的:

    File storefile = new File(storedir +“/”+ mDateTime + “/”+ separator + last_fileName);

你能看到它有什么问题吗?

答案 2 :(得分:0)

使用File类来构建路径更加安全和简单。

以这种方式尝试:

if (!checkExternalMedia()) {
    // show error dialog here.
    return;
}

File storedir = Environment.getExternalStorageDirectory();

String mDateTime = formatter.format(cal.getTime());
File savedir = new File (storedir, mDateTime);

if (fileNameLower.indexOf(".jpeg") > 0) {
    fileExt = ".jpeg";
}
String rand_fileName = Long.toString(System.currentTimeMillis());// +fileExt;
String last_fileName = rand_fileName + fileExt;

File storefile = new File(storedir, last_fileName);

// create all directories required for that file
if (!storefile.mkdirs()) {
    Log.e("Process", "can't create directories:" + storefile.getParent());
    // return here too
}

// here be streams