Android——课堂整理:assets目录和手机外部存储

时间:2022-02-08 10:40:08

layout文件:

 <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="保存资产文件到内部存储"
android:onClick="bt4_onClick"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_1"
android:src="@drawable/on"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="设置图片指向内部存储"
android:onClick="bt5_onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="写入外部存储文件"
android:onClick="bt6_onClick"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取外部存储文件"
android:onClick="bt7_onClick"/>

java类:

 //保存资产文件到内部存储
public void bt4_onClick(View v)
{
try {
//操作assets目录的文件
//1.得到assetsManager
AssetManager am = getAssets();
//2.操作资产目录,边读边写入
//1)读文件到内存 inputstream
InputStream is = am.open("yuantu.png");
//2)写文件到目录 outputstream
FileOutputStream fos = openFileOutput("test.png",MODE_PRIVATE);
//先读后写
byte[] b = new byte[1024];
int i = 0;
while ((i = is.read(b))>0)
{
fos.write(b,0,i);
}
fos.close();
is.close();
Toast.makeText(MainActivity.this, "保存文件成功", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, "保存文件出错", Toast.LENGTH_SHORT).show();
} }
//设置图片指向内部存储
public void bt5_onClick(View v)
{
//1.得到文件路径
String path = getFilesDir().getAbsolutePath()+"/test.png";
Toast.makeText(MainActivity.this, "path = "+path, Toast.LENGTH_SHORT).show();
//2.从内部存储的图片得到Bitmap,BitmapFactory.decodeFile("文件路径");
Bitmap bm = BitmapFactory.decodeFile(path);
//3.设置图片视图的图片来源
iv_1.setImageBitmap(bm);
}
//写入外部存储文件
public void bt6_onClick(View v)
{
//1.判断SD卡是否挂载
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
//得到文本框内容
String str = et_1.getText().toString();
try {
//写入
//1.构造输出流
//1)得到文件路径
//得到SD卡根目录
String path = Environment.getExternalStorageDirectory().getAbsolutePath(); //得到包名对应的目录
// String path = getExternalFilesDir("Music").getCanonicalPath();
Toast.makeText(MainActivity.this, "path = "+path, Toast.LENGTH_LONG).show();
//2)构造
FileOutputStream fos = new FileOutputStream(path+"/test.txt");
PrintStream ps = new PrintStream(fos);
ps.print(str);
ps.close();
fos.close();
Toast.makeText(MainActivity.this, "写入外部文件成功", Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, "存储文件出错", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this, "SD卡没有挂载", Toast.LENGTH_SHORT).show();
}
}
//读取外部存储文件
public void bt7_onClick(View v)
{
//1.判断SD卡是否挂载
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
{
try {
String path = getExternalFilesDir("Music").getCanonicalPath()+"/test.txt";
FileInputStream fis = new FileInputStream(path);
byte[] b = new byte[1024];
int i = 0;
String str = "";
while ((i = fis.read(b))>0)
{
str += new String(b,0,i);
}
fis.close();
Toast.makeText(MainActivity.this, "文件内容 = "+str, Toast.LENGTH_SHORT).show();
}
catch (Exception e)
{
Toast.makeText(MainActivity.this, "读取外部文件失败", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(MainActivity.this, "SD没有挂载", Toast.LENGTH_SHORT).show();
}
}