安卓常見儲存及讀取
阿新 • • 發佈:2019-01-08
一、sp儲存 儲存路徑:/data/data/packageName/shared_prefs/yyy.xml
(隨軟體刪除而刪除,未Root許可權手機不能檢視該路徑)
public class SpUtils {public static final String CACHE_URLS = "cache_urls";
private static SharedPreferences sp;
private static SpUtils instance;
public static SpUtils getInstance(Context context) {
if (instance == null) {
sp = context.getSharedPreferences("xreal", Context.MODE_PRIVATE);
instance = new SpUtils();
}
return instance;
}
/**
* 儲存資料
*
* @param name
* @param value
*/
public void save(String name, Object value) {
if (value instanceof String) {
sp.edit().putString(name, (String) value).apply();
} else if (value instanceof Integer) {
sp.edit().putInt(name, (Integer) value).apply();
} else if (value instanceof Boolean) {
sp.edit().putBoolean(name, (Boolean) value).apply();
}
}
public int getInt(String name, int defValue) {
return sp.getInt(name, defValue);
}
public String getString(String name, String defValue) {
return sp.getString(name, defValue);
}
public boolean getBoolean(String name, Boolean defValue) {
return sp.getBoolean(name, defValue);
}
}
二、內部儲存:儲存路徑: /data/data/projectPackage/files/
(隨軟體刪除而刪除,未Root許可權手機不能檢視該路徑)
FileInputStream fis = openFileInput("logo.png");
儲存檔案
FileOutputStream fos = openFileOutput("logo.png", MODE_PRIVATE)
得到files資料夾物件
File filesDir = getFilesDir();
操作asserts下的檔案
得到AssetManager : context.getAssets();
讀取檔案: InputStream open(filename);
載入圖片檔案
Bitmap BitmapFactory.decodeFile(String pathName) // .bmp/.png/.jpg
三、記憶體卡儲存(外部儲存) 操作許可權:android.permission.WRITE_EXTERNAL_STORAGE
1、不隨軟體刪除而刪除! 儲存路徑: /storage/sdcard/xxx/
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String rootDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + "ScreenRecord" + "/";
File file = new File(rootDir);
if (!file.exists()) {
if (!file.mkdirs()) {
return null;
}
}
return rootDir;
} else {
return null;
}
}
2、隨軟體刪除而刪除! 儲存路徑:/storage/sdcard/Android/data/packageName/files/
public static String getResourceRootPath(Context mAppContext) {
return mAppContext.getExternalFilesDir("VideoCache").getAbsolutePath() + File.separator;
}
四、安卓下載內容到指定目錄儲存:
new Thread(new Runnable() {
@Override
public void run() {
FileOutputStream out = null;
InputStream is = null;
try {
String cacheUrl = downloadStingUrl;
URL url = new URL(cacheUrl);
HttpURLConnection httpConnection = (HttpURLConnection) url.openConnection();
String localUrl = Constants.getResourceRootPath(VideoCacheService.this)+ downloadMovieName;
File cacheFile = new File(localUrl);
out = new FileOutputStream(cacheFile,true);
is = httpConnection.getInputStream();
int mTotalSize = is.available();
int mCurrentSize = 0;
byte buf[] = new byte[4 * 1024];
int len = 0;
Intent mDownloadData = new Intent();
mDownloadData.setAction("xreal.recelerviewtest.service.VideoCacheService");
while ((len = is.read(buf)) != -1) {
try {
out.write(buf, 0, len);
mCurrentSize += len;
mDownloadData.putExtra("totalSize",mTotalSize);
mDownloadData.putExtra("currentSize",mCurrentSize);
sendBroadcast(mDownloadData);
} catch (Exception e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
五、讀取檔案
public String loadFile2String(String fileName) {
StringBuffer strBuffer = new StringBuffer();
try {
InputStream inputStream = new FileInputStream(fileName);
InputStreamReader inputReader = new InputStreamReader(inputStream);
BufferedReader bufferReader = new BufferedReader(inputReader);
String line = null;
while ((line = bufferReader.readLine()) != null) {
strBuffer.append(line);
}
} catch (IOException e) {
System.out.println(e.getMessage());
}
return strBuffer.toString();
}