繁体   English   中英

Android文件未在SD卡上创建

[英]Android File not created on SD card

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

Logcat打印:

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

因此,该文件尚未创建。 我对android清单有正确的权限:

  <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;**
        }

粗体捕获是运行的,并打印:

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

您只需创建对象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()的完整代码吗?

试试吧

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");

   }

我的回答在这里

首先打开路径,然后添加文件:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM