SharedPreferences存储加SD 卡存储

public class sdkActivity extends AppCompatActivity {
    
    
    private static final String TAG = "sdkActivity";
    private Button btnOne;
    private Button btnTwo;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sdk);

        btnOne = (Button) findViewById(R.id.btn_one);
        btnTwo = (Button) findViewById(R.id.btn_two);

        //判断是否有
        String externalStorageState = Environment.getExternalStorageState();
        Log.i(TAG, "onCreate: " + externalStorageState);
        //寻找目录
        File externalStorageDirectory = Environment.getExternalStorageDirectory();

        Log.i(TAG, "onCreate: "+externalStorageDirectory);

        //判断版本号
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
    
    

            String[] str = new String[]{
    
    Manifest.permission.WRITE_APN_SETTINGS,Manifest.permission.READ_CALENDAR};
            requestPermissions(str,100);
        }

        btnOne.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                File file = Environment.getExternalStorageDirectory();

                try {
    
    
                    FileOutputStream fos = new FileOutputStream(new File(file,"ceshi.txt"));
                    fos.write("keke".getBytes());
                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e) {
    
    
                    e.printStackTrace();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }
            }
        });

        btnTwo.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View view) {
    
    
                File file = Environment.getExternalStorageDirectory();
                File file1 = new File(file, "ceshi.txt");

                byte[] bytes = new byte[1024];
                int len = 0;
                try {
    
    
                    FileInputStream fileInputStream = new FileInputStream(file1);

                    while ((len = fileInputStream.read(bytes)) != -1){
    
    
                        String s = new String(bytes, 0, len);
                        Toast.makeText(sdkActivity.this, ""+ s, Toast.LENGTH_SHORT).show();
                    }
                } catch (FileNotFoundException e) {
    
    
                    e.printStackTrace();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                }

            }
        });
    }
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    
    
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
    
    
            Log.i(TAG, "onRequestPermissionsResult:这是可以 ");
        }else {
    
    
            Log.i(TAG, "onRequestPermissionsResult: 这是不可以");
        }
    }
}




<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.linshi">

    <!--权限-->
    <uses-permission android:name="android.permission.READ_CALENDAR"/>
    <uses-permission android:name="android.permission.WRITE_CALENDAR"/>

    <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=".sdkActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".WelcomeActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <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>




public class WelcomeActivity extends AppCompatActivity {
    
    
    private ViewPager welcomeVps;
    private List<Fragment> list;
    private Timer timer;
    private sanFragment disan;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        welcomeVps = (ViewPager) findViewById(R.id.welcome_vps);

        list = new ArrayList<>();

        list.add(new yiFragment());
        list.add(new erFragment());
        list.add(new sanFragment());

        WelcomeAdapter welcomeAdapter = new WelcomeAdapter(getSupportFragmentManager(), list);

        welcomeVps.setAdapter(welcomeAdapter);

        //自动翻页
        timer = new Timer();
        timer.schedule(new TimerTask() {
    
    
            int index = 0;
            @Override
            public void run() {
    
    
                runOnUiThread(new Runnable() {
    
    
                    @Override
                    public void run() {
    
    

                        welcomeVps.setCurrentItem(index);
                        if (++index > list.size()-1){
    
    
                            index = 0;
                        }
                    }
                });


            }
        },0,1000);

        welcomeVps.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
    
    
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    
    

            }

            @Override
            public void onPageSelected(int position) {
    
    

                if (position == list.size()-1){
    
    

                    disan.handler.sendEmptyMessage(101);
                }
            }
            @Override
            public void onPageScrollStateChanged(int state) {
    
    

            }
        });
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_46367373/article/details/104566424