首页 > 解决方案 > 数据仍然保存在内部存储器中,为什么?

问题描述

单击保存按钮时会调用以下方法。但是,数据仍然存储在我的内部存储器中,而不是 sd 卡(外部存储器)中。我是否使用了错误的权限、方法?

public void save(View v)
{
    if(isExternalStorageWriteable() && checkPermission(andriod.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
        
        File textfile = new File(Environment.getExternalStorageDirectory(), "Device 1 ");

        try{
            FileOutputStream fos = new FileOutputStream(textfile);
            fos.write(epc.toString().getBytes());
            fos.close();
            Log.i("State","2");
            Toast.makeText(this, "File saved.", Toast.LENGTH_LONG).show();
        }
        catch (IOException e)
        {
            e.printStackTrace();
            Toast.makeText(this, e.getMessage(), Toast.LENGTH_LONG).show();
        }
    }
    else
    {
        Log.i("State","4");
        Toast.makeText(this, "File not saved.", Toast.LENGTH_LONG).show();
    }
}

private boolean isExternalStorageWriteable()
{
    if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
        Log.i("State","Yes, it is writeable!");
        return true;
    }

    else{
        return false;
    }
}

public boolean checkPermission(String permission){
    int check = ContextCompat.checkSelfPermission(this,permission);
    return (check == PackageManager.PERMISSION_GRANTED);
}

安卓清单

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

标签: androidandroid-studioandroid-external-storage

解决方案


推荐阅读