【问题标题】:Android File not created on SD card未在 SD 卡上创建 Android 文件
【发布时间】:2012-10-08 16:41:07
【问题描述】:

我正在尝试在我的设备上的 SD 上创建一个文件。这在一周前有效,但现在无效,我不明白为什么。

Logcat 打印:

java.io.FileNotFoundException ...pathtofile... (no such file or directory)

所以,文件没有被创建。我对 android manifest 有正确的权限:

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

我是这样创建文件的:

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
           base = Environment.getExternalStorageDirectory().getAbsolutePath();
       }
   String fname = File.separator +"VID_"+ timeStamp + ".3gp";
       mediaFile = new File(base+fname);

然后我检查它是否存在:

   if(mediaFile.exists()){
           Log.v("mediaFile","ex");
       }else{
           Log.v("mediaFile","no ex");

       }

日志说它不存在。我也尝试过使用 file.createNewFile(),但它不起作用。

所以,一周前它可以工作,现在它不能工作,这可能是 SD 卡的问题 ????会不会是某种BUG!!!???

谢谢

编辑:更多代码

创建文件的函数是:

private static File getOutputMediaFile()

调用自:

private static Uri getOutputMediaFileUri(){
         return Uri.fromFile(getOutputMediaFile());
   }

并将 mediarecorder 输出设置为:

vMediaRecorder.setOutputFile(getOutputMediaFileUri().toString());

所以,当我执行 mediarecorder.prepare() 时:

try {
            vMediaRecorder.prepare();

        } catch (IllegalStateException e) {
            Log.v("RELEASE VIDREC1",e.toString());
            releaseMediaRecorder();
            return false;
        } **catch (IOException e) {
            Log.v("RELEASE VIDREC2",e.toString());
            releaseMediaRecorder();
            return false;**
        }

粗体的 catch 是运行并打印的:

java.io.FileNotFoundException ...pathtofile... (no such file or directory)

【问题讨论】:

  • 是的,文件没有创建:(

标签: java android file io


【解决方案1】:

试试这个

String fname = "VID_"+ timeStamp + ".3gp";

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
   mediaFile = new File(android.os.Environment.getExternalStorageDirectory(),fname);
    if(!mediaFile.exists())
    {
         mediaFile.createNewFile();
    }
}

if(mediaFile.exists()){
       Log.v("mediaFile","ex");
   }else{
       Log.v("mediaFile","no ex");

   }

【讨论】:

  • 好的,谢谢,现在 Logcat 说该文件存在,但问题是我将它作为输出文件发送到 Mediarecorder.setoutputfile(uri.tostring()),其中 uri 是路径到文件。当我执行 mediarecorder.prepare 时,io 异常打印没有具有该名称的文件:S.
【解决方案2】:

您只是创建对象 mediaFile,而不是实际文件。使用这个:

if(!f.exists()){
  f.createNewFile();
}

我试过了,对我来说它有效。

final String filename = "file.3gp";
final String path = Environment.getExternalStorageDirectory() + "/" + filename;
File outputfile = new File(path);
if (!outputfile.exists()) {
    try {
        outputfile.createNewFile();
    } 
    catch (IOException e) {
        e.printStackTrace();
    }
}
MediaRecorder recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(outputfile.toString());
try {
    recorder.prepare();
    recorder.start();
    /*recorder.stop();
    recorder.reset();
    recorder.release();*/
} 
catch (IllegalStateException e) {
    e.printStackTrace();
} 
catch (IOException e) {
    e.printStackTrace();
}

试试看它是否有效。如果没有,能否添加getOutputMediaFile()的完整代码?

【讨论】:

    【解决方案3】:

    我的回答here

    先打开路径,再添加文件:

    String dir = Environment.getExternalStorageDirectory(); // getAbsolutePath is not requried
    File path = new File(dir);
    File root = new File(path,  "VID_"+ timeStamp + ".3gp";);
    

    【讨论】:

    • 好的,试过了,但是修改了 String dir = Environment.getExternalStorageDirectory().toString();我得到了与 Sumant 答案相同的结果(请参阅我的评论)。
    【解决方案4】:

    感谢您的帮助,我已经使用旧代码解决了这个问题。这很奇怪,因为“文件保存”部分没有修改。谢谢大家,有点。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-04-18
      • 2023-03-13
      • 2018-05-18
      • 2020-04-10
      • 2013-07-24
      • 2011-08-30
      • 1970-01-01
      相关资源
      最近更新 更多