2

デバイスの SD にファイルを作成しようとしています。これは 1 週間前には機能していましたが、現在は機能していません。その理由がわかりません。

Logcat は以下を出力します。

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

したがって、ファイルは作成されていません。私はアンドロイドマニフェストに正しい権限を持っています:

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

       }

ログには、IT DOESN'T EXIST と書かれています。私も file.createNewFile() を試しましたが、うまくいきません。

それで、一週間前は動いていたのに、今は動かないのです。SDカードに問題があるのか​​もしれません???? それはある種のバグである可能性があります!!!????

ありがとう

編集:その他のコード

ファイルが作成される関数は次のとおりです。

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)
4

4 に答える 4

1

実際のファイルではなく、オブジェクト 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() の完全なコードを追加できますか?

于 2012-10-18T09:13:33.460 に答える
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");

   }
于 2012-10-18T09:15:23.317 に答える
0

あなたの助けに感謝します、私は古いコードを使って問題を解決しました。「ファイル保存」部分に変更がないので不思議です。みんなありがとう、ちょっと。

于 2012-10-19T08:18:57.243 に答える
0

ここでの私の答え:

最初にパスを開き、次にファイルを追加します。

String dir = Environment.getExternalStorageDirectory(); // getAbsolutePath is not requried
File path = new File(dir);
File root = new File(path,  "VID_"+ timeStamp + ".3gp";);
于 2012-10-18T09:19:43.893 に答える