读取sd卡
1111吧
全部回复
仅看楼主
level 1
btnReadSD.setOnClickListener (new View.OnClickListener()
eoverride
public void onClick(Viewv){
String state= Environment.getExternalStorageStateO if(state. equals(Environment. MEDIA MOUNTED )
File SDPath-Environment.getExternalStorageDirectorr() File f=new File(SDPath,child:"poem2.txt") String str try
FileInputStream fis=ncw FileInputStream(f), byte. buffer-new byte[lbk]fis.available(l fis.reed(buffer)
str-new String(buffer) txtDisplay. setText (str) catch (Exception e3){
Toast. makeText( context MainActivity.this. text “so卡读取出现问!",Toast.LENGTH LONG), show ()
2025年11月14日 06点11分 1
level 1
btnWriteSP.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
网页链接 editor = sp.edit();
//利用编辑器,封装数据
editor.putString("userName", "张长明");
editor.putInt("userAge", 36);
editor.commit();
Toast.makeText(MainActivity.this, "SP完成数据存储!", Toast.LENGTH_LONG).show();
}
});
2025年11月14日 07点11分 2
level 1
package com.example.firstapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
public class MainActivity extends AppCompatActivity {
private Button btnWriteDisk,btnReadDisk,btnWriteSD,btnReadSD,btnWriteSp,btnReadSP,btnJSON;
private TextView txtDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnWriteDisk=(Button)findViewById(R.id.button);
btnReadDisk=(Button)findViewById(R.id.button2);
btnWriteSD=(Button)findViewById(R.id.button3);
btnReadSD=(Button)findViewById(R.id.button4);
btnWriteSp=(Button)findViewById(R.id.button5);
btnReadSP=(Button)findViewById(R.id.button6);
btnJSON=(Button)findViewById(R.id.button7);
txtDisplay=(TextView)findViewById(R.id.textView);
btnWriteDisk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str=" 出塞\r\n\r\n 唐·王昌龄\r\n\r\n秦时明月汉时关,\r\n\r\n万里长征人未还。\r\n\r\n但使龙城飞将在,\r\n\r\n不教胡马度阴山。";
try{
FileOutputStream fos=openFileOutput("poeml.txt",MODE_PRIVATE);
fos.write(str.getBytes());
fos.close();
Toast.makeText(MainActivity.this,"硬盘存储文件成功!",Toast.LENGTH_LONG).show();
}catch (Exception el) {
Toast.makeText(MainActivity.this, "硬盘存储文件出现问题!",Toast.LENGTH_LONG).show();
}
}
});
btnReadDisk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String str="";
try{
FileInputStream fis=openFileInput("poeml.txt");
byte[] buffer=new byte[fis.available()];
fis.read(buffer);
str=new String(buffer);
fis.close();
txtDisplay.setText(str);
}catch (Exception e2) {
Toast.makeText(MainActivity.this, "读取硬盘文件出现问题!", Toast.LENGTH_LONG).show();
}
}
});
btnWriteSD.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String state= Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)){
File SDPath = Environment.getExternalStorageDirectory();
File f = new File(SDPath, "poem2");
String str = " 绝句\r\n\r\n 唐·杜甫\r\n\r\n两个黄鹂鸣翠柳,\r\n\r\n一行白鹭上青天。\r\n\r\n窗含西岭千秋雪,\r\n\r\n门泊东吴万里船。";
try {
FileOutputStream fos = new FileOutputStream(f);
fos.write(str.getBytes());
fos.close();
Toast.makeText(MainActivity.this, "SD卡存储文件成功!", Toast.LENGTH_LONG).show();
} catch (Exception e3) {
Toast.makeText(MainActivity.this, "SD卡存储文件出现问题!", Toast.LENGTH_LONG).show();
}
}
}
});
btnReadSD.setOnClickListener (new View.OnClickListener(){
@Override
public void onClick(View v){
String state= Environment.getExternalStorageState();
if(state.equals(Environment.MEDIA_MOUNTED )){
File SDPath = Environment.getExternalStorageDirectory();
File f = new File(SDPath,"poem2");
String str;
try {
FileInputStream fis = new FileInputStream(f);
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
str=new String(buffer);
txtDisplay.setText(str);
} catch (Exception e3) {
Toast.makeText(MainActivity.this,"sd卡读取出现问 !", Toast.LENGTH_LONG).show();
}
}
}
});
btnWriteSp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("userName", "张长明");
editor.putInt("userAge", 36);
editor.commit();
Toast.makeText(MainActivity.this, "SP完成数据存储!", Toast.LENGTH_LONG).show();
}
});
}
}
2025年11月14日 07点11分 3
level 1
<?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=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="10dp"
android:text="存硬盘"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="95dp"
android:text="读硬盘"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="180dp"
android:text="存SD卡"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginLeft="265dp"
android:text="读SD卡"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginLeft="10dp"
android:text="存SP"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginLeft="95dp"
android:text="读SP"
android:textColor="#009688"
android:textSize="18sp" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:layout_marginLeft="180dp"
android:text="JSON解析"
android:textColor="#009688"
android:textSize="18sp" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="TextView"
android:textColor="#9C27B0"
android:textSize="20sp" />
</RelativeLayout>
2025年11月14日 07点11分 4
1