在保存Environment.getExternalStoragepublicdirectory时遇到错误

时间:2015-08-13 17:41:54

标签: android

package com.example.askselva.sampledatabase;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;


public class HomeActivity extends AppCompatActivity{

    EditText UserName;
    EditText PassWord;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        UserName=(EditText) findViewById(R.id.EtUserName);
    }
    public void save(View view){
        SharedPreferences sharedPreferences=getSharedPreferences("MyData", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();
        editor.putString("UserName",UserName.getText().toString());
        editor.putString("Password",PassWord.getText().toString());
Toast.makeText(this,PassWord.getText().toString(),Toast.LENGTH_LONG).show();
        editor.commit();
        Toast.makeText(this,"Data Saved Successfully",Toast.LENGTH_LONG).show();
    }
    public void saveCache(View view) {
        String data=UserName.getText().toString();
        File folder=getCacheDir();
        File myFile=new File(folder,"MyData1.txt");
        writeData(myFile, data);
    }
    public void saveExternalCache(View view) {
        String data=UserName.getText().toString();
        File folder=getExternalCacheDir();
        File myFile=new File(folder,"MyData2.txt");
        writeData(myFile, data);
    }
    public void saveExternalFile(View view) {
        String data=UserName.getText().toString();
        File folder=getExternalFilesDir("SampleDataBase");
        File myFile=new File(folder,"MyData3.txt");
        writeData(myFile, data);
    }
    public void savePublicExternalFile(View view) {
        try {
            String data = UserName.getText().toString();

            if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
                String folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getAbsolutePath();
                Log.i("Check","Data->"+folder);
                File myFile = new File(folder, "MyData4.txt");
                Toast.makeText(this, "" + myFile, Toast.LENGTH_LONG).show();
                writeData(myFile, data);
            }
        }
        catch(Exception e){
            Toast.makeText(this,"Error :"+e,Toast.LENGTH_LONG).show();
        }

    }
    public void writeData(File myFile,String data){
        FileOutputStream fileOutputStream=null;
        try{
            fileOutputStream=new FileOutputStream(myFile);
            fileOutputStream.write(data.getBytes());
            Toast.makeText(this,data+" was Stored Successfully "+myFile.getAbsolutePath(),Toast.LENGTH_LONG).show();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            try {
                if(fileOutputStream!=null) {
                    fileOutputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
    public void next(View view){
      Toast.makeText(this,"Next",Toast.LENGTH_LONG).show();
        Intent intent=new Intent(this, ActivityB.class);
        startActivity(intent);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_home, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

这是我的代码。我正在

08-13 23:09:12.005 5849-5849 / com.example.askselva.sampledatabase W / System.err:java.io.FileNotFoundException:/storage/sdcard/Documents/MyData4.txt:open failed:ENOENT(没有相应的文件和目录) 08-13 23:09:12.005 5849-5849 / com.example.askselva.sampledatabase W / System.err:at libcore.io.IoBridge.open(IoBridge.java:456) 08-13 23:09:12.006 5849-5849 / com.example.askselva.sampledatabase W / System.err:at java.io.FileOutputStream。(FileOutputStream.java:87) 08-13 23:09:12.006 5849-5849 / com.example.askselva.sampledatabase W / System.err:at java.io.FileOutputStream。(FileOutputStream.java:72) 08-13 23:09:12.006 5849-5849 / com.example.askselva.sampledatabase W / System.err:at com.example.askselva.sampledatabase.HomeActivity.writeData(HomeActivity.java:81) 08-13 23:09:12.006 5849-5849 / com.example.askselva.sampledatabase W / System.err:at com.example.askselva.sampledatabase.HomeActivity.savePublicExternalFile(HomeActivity.java:70) 08-13 23:09:12.007 5849-5849 / com.example.askselva.sampledatabase W / System.err:at java.lang.reflect.Method.invoke(Native Method) 08-13 23:09:12.007 5849-5849 / com.example.askselva.sampledatabase W / System.err:at java.lang.reflect.Method.invoke(Method.java:372) 08-13 23:09:12.009 5849-5849 / com.example.askselva.sampledatabase W / System.err:at android.view.View $ 1.onClick(View.java:4002) 08-13 23:09:12.009 5849-5849 / com.example.askselva.sampledatabase W / System.err:at android.view.View.performClick(View.java:4756) 08-13 23:09:12.009 5849-5849 / com.example.askselva.sampledatabase W / System.err:at android.view.View $ PerformClick.run(View.java:19749) 08-13 23:09:12.010 5849-5849 / com.example.askselva.sampledatabase W / System.err:at android.os.Handler.handleCallback(Handler.java:739) 08-13 23:09:12.011 5849-5849 / com.example.askselva.sampledatabase W / System.err:at android.os.Handler.dispatchMessage(Handler.java:95) 08-13 23:09:12.011 5849-5849 / com.example.askselva.sampledatabase W / System.err:at android.os.Looper.loop(Looper.java:135) 08-13 23:09:12.011 5849-5849 / com.example.askselva.sampledatabase W / System.err:at android.app.ActivityThread.main(ActivityThread.java:5221) 08-13 23:09:12.011 5849-5849 / com.example.askselva.sampledatabase W / System.err:at java.lang.reflect.Method.invoke(Native Method) 08-13 23:09:12.011 5849-5849 / com.example.askselva.sampledatabase W / System.err:at java.lang.reflect.Method.invoke(Method.java:372) 08-13 23:09:12.011 5849-5849 / com.example.askselva.sampledatabase W / System.err:at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899) 08-13 23:09:12.011 5849-5849 / com.example.askselva.sampledatabase W / System.err:at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 08-13 23:09:12.011 5849-5849 / com.example.askselva.sampledatabase W / System.err:引起:android.system.ErrnoException:open failed:ENOENT(没有这样的文件或目录) 08-13 23:09:12.011 5849-5849 / com.example.askselva.sampledatabase W / System.err:at libcore.io.Posix.open(Native Method) 08-13 23:09:12.012 5849-5849 / com.example.askselva.sampledatabase W / System.err:at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186) 08-13 23:09:12.012 5849-5849 / com.example.askselva.sampledatabase W / System.err:at libcore.io.IoBridge.open(IoBridge.java:442) 08-13 23:09:12.012 5849-5849 / com.example.askselva.sampledatabase W / System.err:... 17更多 误差..

我如何解决这个错误请帮助我..

1 个答案:

答案 0 :(得分:2)

在清单中设置以下权限:

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