2017-08-20 5 views
-4

私は、携帯電話の内蔵ストレージメモリを使用するアプリケーションを作成しようとしています。私はWRITETOFILE方法にアプリのクラッシュを使用しようとすると内部ストレージ内のファイルに書き込もうとするとアプリケーションがクラッシュする - Android Studio

public void writeToFile(String data,String name, Context context) { 
    try { 
     FileOutputStream fos = openFileOutput(name, Context.MODE_PRIVATE); 
     fos.write(data.getBytes()); 
     fos.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

: は、これは私の方法は次のように探しているものです。

[編集]

これは何かを意味していますか?

08-20 17:32:04.301 921-921/com.adamcomp.websitecreator E/AndroidRuntime: FATAL EXCEPTION: main 
                    Process: com.adamcomp.websitecreator, PID: 921 
                    java.lang.IllegalArgumentException: File android.support.v7.widget.AppCompatEditText{5211a9d VFED..CL. .F...... 150,514-570,605 #7f0d007c app:id/prjname}_code.txt contains a path separator 
                     at android.app.ContextImpl.makeFilename(ContextImpl.java:2322) 
                     at android.app.ContextImpl.openFileOutput(ContextImpl.java:466) 
                     at android.content.ContextWrapper.openFileOutput(ContextWrapper.java:197) 
                     at com.adamcomp.websitecreator.NewProject.writeToFile(NewProject.java:75) 
                     at com.adamcomp.websitecreator.NewProject$1.onClick(NewProject.java:60) 
                     at android.view.View.performClick(View.java:5721) 
                     at android.widget.TextView.performClick(TextView.java:10949) 
                     at android.view.View$PerformClick.run(View.java:22624) 
                     at android.os.Handler.handleCallback(Handler.java:739) 
                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                     at android.os.Looper.loop(Looper.java:148) 
                     at android.app.ActivityThread.main(ActivityThread.java:7407) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
+3

editText.toString() 

変更、それをあなたが 'e.printStackTrace()の出力を提供する必要が持っている場合、'あなたの問題を解決するlogcatからを。 – Kiskae

+1

logcatエラー –

+1

には、スタックトレースと 'writeToFile'メソッドのコードを提供します。 –

答えて

0
java.lang.IllegalArgumentException: File android.support.v7.widget.AppCompatEditText{5211a9d VFED..CL. .F...... 150,514-570,605 #7f0d007c app:id/prjname}_code.txt contains a path separator 

EditTexttoString()をファイル名として渡しているようですが、この文字列表現には無効な文字が含まれています。

代わりに、EditText内のテキストコンテンツを取得するには、Editableを取得し、その上toString()を呼び出すためにgetText()を呼び出します。ですから、今、非常に少なくとも

editText.getText().toString() 
+0

のコードを書くことができますか?私はあなたが "toString()を呼び出す"ことを意味するのか分かりません。 –

+0

それは今動作します –

-2

たぶん、あなたはあなたがデータディレクトリにファイルを保存しようとしている場合は、これを試してみてくださいにIOException

+0

IOExceptionが例外を拡張することはご存知ですか? –

+0

はい私は彼がプログラムを実行している間にランタイムに彼を放置しないので、確信しています –

-1

を使用してみてください例外を使用している、ここで

} catch (IOException ex) { 
    ex.printStackTrace(); 
    return null; 
} 

を問題を抱えている:

public void save(String data, String folder, String fileName, Context c) { 
    File Folder = new File(c.getFilesDir() + File.separator + folder); 
    if (!Folder.mkdir()) { 

    } 
    //Make sure not to include a path separator("/") 
    //Just enter the path name. 
    //e.g If you want to store the file in a folder named myFolder, and you file name is myFile 
    //Then use the function like this: save(your data,"myFolder","myFile",context); 

    File file = new File(c.getFilesDir() + File.separator 
      + folder + File.separator + fileName); 


    try { 
     if (!file.createNewFile()) { 
     } 

     OutputStream osw = new FileOutputStream(file, true); 

     osw.write(data.getBytes()); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 
+0

ここではlogcat [リンク](https://pastebin.com/SE8dJ1NV) –

関連する問題