为什么这个Android程序在调用save后会崩溃?

时间:2018-04-13 00:14:46

标签: java android

XML

<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
              package="com.example.adufordjour.external">
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
                         android:maxSdkVersion="18"/>

        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
   </manifest>

   <?xml version="1.0" encoding="utf-8"?>
   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       tools:context="com.example.adufordjour.external.MainActivity">

       <TextView
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentStart="true"
           android:layout_alignParentTop="true"
           android:layout_marginStart="16dp"
           android:layout_marginTop="61dp"
           android:text="NAME"
           android:id="@+id/textView"
           />

       <EditText
           android:id="@+id/editText"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignParentTop="true"
           android:layout_marginStart="32dp"
           android:layout_marginTop="44dp"
           android:layout_toEndOf="@+id/textView"
           android:ems="10"
           android:inputType="textPersonName"
           android:text="Name" />

       <Button
           android:id="@+id/button"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:layout_alignStart="@+id/editText"
           android:layout_centerVertical="true"
           android:text="Save"
           android:onClick="sAVE"/>
   </RelativeLayout>

Android代码

这是用于在公共外部设备上存储数据的代码:

package com.example.adufordjour.external;

    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    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 MainActivity extends AppCompatActivity {
        EditText editText1;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            editText1 = findViewById(R.id.editText);
        }

这是保存到公共外部设备的方法

        public void sAVE(View view){
            String st1 = editText1.getText().toString();
            File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "myFile");
            FileOutputStream fileOutputStream=null;
            try {
                if (isExternalStorageWritable()==true){
                    fileOutputStream =new FileOutputStream(file);
                    fileOutputStream.write(st1.getBytes());}
                else {
                    Toast.makeText(this, "not saved", Toast.LENGTH_SHORT).show();
                }
            }
            catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                try {
                    fileOutputStream.close();
                }
                catch (IOException e) {
                    e.printStackTrace();
                }
            }
            Toast.makeText(this, "file save at" + " " + "myFile", Toast.LENGTH_SHORT).show();
        }

        public boolean isExternalStorageWritable(){
            String state = Environment.getExternalStorageState();
            if (Environment.MEDIA_MOUNTED.equals(state)) {
                return true;
            }
            else {
                return false;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

为什么在android:maxSdkVersion="18"中使用uses-permission?我认为那里不需要它。

尝试添加READ_EXTERNAL_STORAGE权限并检查其是否有效。如果没有,请粘贴崩溃的堆栈跟踪。

相关问题