gpt4 book ai didi

java - 在 Sdcard Android 中写入时找不到文件异常

转载 作者:行者123 更新时间:2023-11-30 03:33:02 28 4
gpt4 key购买 nike

这是我的代码

public class MainActivity extends Activity {
private String PRENOM = "prenom.txt";
private String userName = "Apollidore";
private File mFile = null;

private Button mWrite = null;
private Button mRead = null;
String state = Environment.getExternalStorageState();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);// On crée un fichier qui correspond à l'emplacement extérieur
mFile = new File(Environment.getExternalStorageDirectory().getPath()+ "/Android/data/ " +getPackageName()+ "/files/" + PRENOM);


mWrite = (Button) findViewById(R.id.write);
mWrite.setOnClickListener(new View.OnClickListener() {


public void onClick(View pView) {
try {
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
Log.v("state", "mounted");
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
Log.v("state", "read only");
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
Log.v("state", "gg");
}
// Flux interne
FileOutputStream output = openFileOutput(PRENOM, MODE_WORLD_WRITEABLE);

// On écrit dans le flux interne
// output.write(userName.getBytes());

/* if(output != null)
output.close();*/

// Si le fichier est lisible et qu'on peut écrire dedans
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
&& !Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState())) {
Log.v("state" ,"enter");
mFile.createNewFile(); if ( mFile.createNewFile())Log.v("file", "created");
output = new FileOutputStream(mFile);
output.write(userName.getBytes());
if(output != null)
output.close();
}
else {
Toast.makeText(MainActivity.this, Environment.getExternalStorageState(), Toast.LENGTH_SHORT).show();

}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});

mRead = (Button) findViewById(R.id.read);
mRead.setOnClickListener(new View.OnClickListener() {

public void onClick(View pView) {
try {
FileInputStream input = openFileInput(PRENOM);
int value;
// On utilise un StringBuffer pour construire la chaîne au fur et à mesure
StringBuffer lu = new StringBuffer();
// On lit les caractères les uns après les autres
while((value = input.read()) != -1) {
// On écrit dans le fichier le caractère lu
lu.append((char)value);
}
// Toast.makeText(MainActivity.this, "Interne : " + lu.toString(), Toast.LENGTH_SHORT).show();
if(input != null)
input.close();

if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {

Log.v("state" ,"enter");
lu = new StringBuffer();
input = new FileInputStream(mFile);
while((value = input.read()) != -1)
lu.append((char)value);
Toast.makeText(MainActivity.this, "Externe : " + lu.toString(), Toast.LENGTH_SHORT).show();
if(input != null)
input.close();
}
else
{
Toast.makeText(MainActivity.this, Environment.getExternalStorageState(), Toast.LENGTH_SHORT).show();

}

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
}

日志文件:

   06-16 11:15:36.380: V/state(5335): mounted
06-16 11:15:36.380: V/state(5335): enter
06-16 11:15:36.380: W/System.err(5335): java.io.IOException: No such file or directory
06-16 11:15:36.380: W/System.err(5335): at java.io.File.createNewFileImpl(Native Method)
06-16 11:15:36.380: W/System.err(5335): at java.io.File.createNewFile(File.java:1257)
06-16 11:15:36.380: W/System.err(5335): at com.example.testprediction.MainActivity$1.onClick(MainActivity.java:63)
06-16 11:15:36.380: W/System.err(5335): at android.view.View.performClick(View.java:2506)
06-16 11:15:36.380: W/System.err(5335): at android.view.View$PerformClick.run(View.java:9112)
06-16 11:15:36.380: W/System.err(5335): at android.os.Handler.handleCallback(Handler.java:587)
06-16 11:15:36.380: W/System.err(5335): at android.os.Handler.dispatchMessage(Handler.java:92)
06-16 11:15:36.380: W/System.err(5335): at android.os.Looper.loop(Looper.java:130)
06-16 11:15:36.380: W/System.err(5335): at android.app.ActivityThread.main(ActivityThread.java:3835)
06-16 11:15:36.390: W/System.err(5335): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 11:15:36.390: W/System.err(5335): at java.lang.reflect.Method.invoke(Method.java:507)
06-16 11:15:36.390: W/System.err(5335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
06-16 11:15:36.390: W/System.err(5335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
06-16 11:15:36.390: W/System.err(5335): at dalvik.system.NativeStart.main(Native Method)

06-16 11:15:45.420: V/state(5335): mounted
06-16 11:15:45.430: V/state(5335): enter
06-16 11:15:45.430: W/System.err(5335): java.io.IOException: No such file or directory
06-16 11:15:45.430: W/System.err(5335): at java.io.File.createNewFileImpl(Native Method)
06-16 11:15:45.430: W/System.err(5335): at java.io.File.createNewFile(File.java:1257)
06-16 11:15:45.430: W/System.err(5335): at com.example.testprediction.MainActivity$1.onClick(MainActivity.java:63)
06-16 11:15:45.430: W/System.err(5335): at android.view.View.performClick(View.java:2506)
06-16 11:15:45.430: W/System.err(5335): at android.view.View$PerformClick.run(View.java:9112)
06-16 11:15:45.430: W/System.err(5335): at android.os.Handler.handleCallback(Handler.java:587)
06-16 11:15:45.430: W/System.err(5335): at android.os.Handler.dispatchMessage(Handler.java:92)
06-16 11:15:45.430: W/System.err(5335): at android.os.Looper.loop(Looper.java:130)
06-16 11:15:45.430: W/System.err(5335): at android.app.ActivityThread.main(ActivityThread.java:3835)
06-16 11:15:45.430: W/System.err(5335): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 11:15:45.430: W/System.err(5335): at java.lang.reflect.Method.invoke(Method.java:507)
06-16 11:15:45.440: W/System.err(5335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
06-16 11:15:45.440: W/System.err(5335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)

编辑

a 将 mFile 的声明更改为

     mFile=new File(getApplicationContext().getExternalFilesDir(null).getName()+PRENOM) 

并且我通过 NullPointer 异常添加了那些行

   if(!mFile.getParentFile().exists())
{
mFile.getAbsoluteFile().mkdirs();
}

最佳答案

在调用 createNewFile 之前获取您的文件的父文件并从中调用 mkdirs() 方法。

还可以使用 context.getExternalFilesDir()context.getExternalCacheDir()。看起来您正在尝试将数据保存到这些目录之一,这是获取它们路径的更正确的方法。

关于java - 在 Sdcard Android 中写入时找不到文件异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17132224/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com