編輯:關於Android編程
Environment.getExternalStorageDirectory()判斷sdcard狀態:
Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)保存到sd卡: filename 文件名 content內容
public void saveToSDCard(String filename,String content) throws Exception{
File file=new File(Environment.getExternalStorageDirectory(), filename);
OutputStream out=new FileOutputStream(file);
out.write(content.getBytes());
out.close();
}
相關權限添加:
/** 保存方法 */
public void saveBitmap(Bitmap bm, String picName) {
Log.e(TAG, "保存圖片");
File f = new File(Environment.getExternalStorageDirectory(), picName);//保存路徑和圖片名稱(上文說的日期和時間可以作為)
if (f.exists()) {
f.delete();
}
try {
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.PNG, 90, out);
out.flush();
out.close();
Log.i(TAG, "已經保存");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
同樣存入數據庫,檢索數據庫。
(1).要先設置緩存圖片的內存大小,基本上設置為手機內存的1/8,
手機內存的獲取方式:int MAXMEMONRY = (int) (Runtime.getRuntime() .maxMemory() / 1024);
(2).LruCache裡面的鍵值對分別是URL和對應的圖片;
(3).重寫了一個叫做sizeOf的方法,返回的是圖片數量。
import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
/**
* application
* @author hao
*
*/
public class MyApplication extends Application{
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
}
/**
* @description
*
* @param context
* @return 得到需要分配的緩存大小,這裡用八分之一的大小來做
*/
public int getMemoryCacheSize() {
// Get memory class of this device, exceeding this amount will throw an
// OutOfMemory exception.
final int memClass = ((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
// Use 1/8th of the available memory for this memory cache.
return 1024 * 1024 * memClass / 8;
}
}
ps:這個方法可以說就是獲取系統分配緩存的大小/8 說明我們用其中的八分之一來做緩存,建議配置在application裡面,方便調用。
final int memoryCache = ((KaleApplication) getApplication()).getMemoryCacheSize();
Log.d(TAG, "cache size = " + memoryCache / 1024 / 1024 + "M");
mMemoryCache = new LruCache(memoryCache) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// 重寫此方法來衡量每張圖片的大小,默認返回圖片數量。
return bitmap.getByteCount() / 1024;
}
}; // 初始化
ps:獲取全局配置的內存,我通過緩存的值來初始化了cache對象,然後重寫了sizeOf()方法。(返回數量)
/**
* @description 將bitmap添加到內存中去
*
* @param key
* @param bitmap
*/
public void addBitmapToMemoryCache(String key, Bitmap bitmap) {
if (getBitmapFromMemCache(key) == null) {
mMemoryCache.put(key, bitmap);
}
}
/**
* @description 通過key來從內存緩存中獲得bitmap對象
*
* @param key
* @return
*/
private Bitmap getBitmapFromMemCache(String key) {
return mMemoryCache.get(key);
}
/**
* @description 將bitmap加載到imageview中去
*
* @param resId
* @param imageView
*/
public void loadBitmapToImageView(String url, ImageView imageView) {
final Bitmap bitmap = getBitmapFromMemCache("img"); // 先看這個資源在不在內存中,如果在直接讀取為bitmap,否則返回null
if (bitmap != null) {
Log.d(TAG, "in memory");
imageView.setImageBitmap(bitmap);
} else {
Log.d(TAG, "not in memory");
imageView.setImageResource(R.drawable.ic_launcher); // 如果沒有在內存中,先顯示默認的圖片,然後啟動線程去下載圖片
BitmapWorkerTask task = new BitmapWorkerTask(imageView);
task.execute(url); // 啟動線程,從網絡下載圖片,下載後加入緩存
}
}
ps:判斷圖片是否已經進行了緩存操作。圖片如果在內存中就直接賦值,沒有啟動線程重新獲取。
import java.io.IOException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.widget.ImageView; public class BitmapWorkerTask extends AsyncTaskps:得到後將bitmap放入緩存中,最後在imageview中展示。 讓我們最後看一下效果吧!!! -------------------csdn服務器維護---------------最後再補充! 點擊下載圖片,小機器人加載中,然後下載從網絡下載成功!再次點擊則不會加載,從內存中獲取!{ private MainActivity mActivity; private ImageView mImageView; public BitmapWorkerTask(ImageView imageView) { mImageView = imageView; mActivity = (MainActivity) imageView.getContext(); } /** * 下載圖片 */ @Override protected Bitmap doInBackground(String... params) { Bitmap bitmap = null; HttpURLConnection con = null; try { URL url = new URL(params[0]); con = (HttpURLConnection) url.openConnection(); con.setConnectTimeout(10 * 1000); con.setReadTimeout(10 * 1000); bitmap = BitmapFactory.decodeStream(con.getInputStream()); //添加到內存 mActivity.addBitmapToMemoryCache("img", bitmap); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (con != null) { con.disconnect(); } } return bitmap; } @Override protected void onPostExecute(Bitmap result) { super.onPostExecute(result); if (result != null) { mImageView.setImageBitmap(result); } } }
【Android】GreenDao 3.X 結合Volley以及Gson、ImageLoader實現數據存儲
關於GreenDao的優點已經不用再說了,關於第三方數據庫框架有很多,相對於Android系統本身的SQLite以及其它第三方而言,我感覺GreenDao使用更方便,體積
Android實現蒙板效果
本文實例為大家分享了Android實現蒙板效果的相關代碼,供大家參考,具體內容如下1、不保留標題欄蒙板的實現效果:原理:1、彈窗時,設置背景窗體的透明度2、取消彈窗時,恢
Android之Socket,Service通訊
Android與服務器之間的通訊方式主要有兩種。一種是Http通訊 , 一種是Socket通訊。兩者最大的差異在於,Http連接使用的是“請求---響應方式&
仿今日頭條的(一)
頭部用的是TabLayout和ViewPager實現的 底部用的是FragmentTabHost和Fragment實現的先看底部的實現:底部布局: <fram