内部に保存されていますか、それとも外部に保存されていますか?
内部ストレージ
外部ストレージ
1.承認関連の設定
外部ストレージへのアクセス:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
sdにインストール:
android:installLocation="preferExternal"
用的位置:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jsc4.aboutactivity"
android:installLocation="preferExternal" 这里
>
2.内部ストレージ
getFileDir() 返回一个File,代表了我们app的internal目录
getCacheDir() 返回一个File,代表了我们app的internal缓存目录
使用する:
/**
* 测试文件
*/
private void testFileDemo() {
// 在内部存储中创建新文件test.txt
File file = new File(getFilesDir(), "test.txt");
Log.i(TAG, "testFileDemo:getFilesDir():"+ getFilesDir().getAbsolutePath());
Log.i(TAG, "testFileDemo:file path:"+ file.getAbsolutePath());
// getFilesDir():/data/user/0/com.jsc4.aboutactivity/files
// file path:/data/user/0/com.jsc4.aboutactivity/files/test.txt
try {
boolean isSuccess = file.createNewFile();
} catch (IOException e) {
Log.i(TAG, "test.txt create error: "+ e.toString());
e.printStackTrace();
}
String str = "随便写点什么";
try {
FileOutputStream fileOutputStream = openFileOutput("test2.txt", Context.MODE_PRIVATE);
fileOutputStream.write(str.getBytes());
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
3.外部ストレージ、SDカードのステータスを確認します
String state = Environment.getExternalStorageState();
if(TextUtils.equals(state, Environment.MEDIA_MOUNTED)){
}
4.さまざまなディレクトリのファイルを読み取ります
アセット:
コンパイル時にそのままapkに配置さ
れ、R.javaファイルのアセットにマップされません。ディレクトリ構造を再度確立できます。
void testAssets(){
// 直接读路径
WebView webView = new WebView(this);
webView.loadUrl("file:///android_asset/test.html");
// 直接读入流
try {
// open的只能是文件,不能是文件夹
InputStream inputStream = getResources().getAssets().open("test.html");
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "文件读取异常", Toast.LENGTH_LONG).show();
}
// 读取文件名列表
try {
String[] fileNames = getAssets().list("images/");
Log.i(TAG, "testAssets: "+ Arrays.toString(fileNames));
} catch (IOException e) {
e.printStackTrace();
}
// 读一张图片并显示
try {
InputStream inputStream = getAssets().open("images/square32green.png");
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
// 读音频
try {
Log.i(TAG, "testAssets: 播放音乐");
AssetFileDescriptor assetFileDescriptor = getAssets().openFd("lovelove.mps");
MediaPlayer player = new MediaPlayer();
player.reset();
player.setDataSource(
assetFileDescriptor.getFileDescriptor(),
assetFileDescriptor.getStartOffset(),
assetFileDescriptor.getLength());
player.prepare();
player.start();
} catch (IOException e) {
e.printStackTrace();
}
}
raw:
コンパイル時にapkにもそのまま配置されます
が、R.javaファイルのrawにマップされ、ディレクトリ構造を確立できなくなります。
void testResFile(){
InputStream inputStream = getResources().openRawResource(R.raw.lovelove);
getResources().getColor(R.color.colorAccent);
getResources().getString(R.string.age);
getResources().getDrawable(R.drawable.ic_launcher_background);
}
SDカード:
File file = new File("/sdcard/test/a.txt"); 这里的SD卡路径未必正确
SD卡路径一般通过这个方法去获取
File tempFile = Environment.getExternalStorageDirectory().getAbsoluteFile();
其他获取路径的方法:
Environment.getDataDirectory(); 获取Android中的data数据目录
Environment.getDownloadCacheDirectory();
Environment.getExternalStorageDirectory();