在android中建立bitmap避免記憶體不足的方法,androidbitmap
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.miz.heapsize" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="11" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:largeHeap="true" > <activity android:label="@string/app_name" android:name=".MainActivity" > <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>
只需要加上<pre name="code" class="html">android:largeHeap="true"
可以產生48M的 哦耶
http://www.google.com/url?q=http%3A%2F%2Fstackoverflow.com%2Fquestions%2F8675037%2Fdoes-androids-large-heap-option-work-for-older-phones-upgraded-to-ics&sa=D&sntz=1&usg=AFQjCNE43aD9kOt0M5OUzNWaNGXP5pSM8w
解決問題,android記憶體溢出,代碼使用過線程與bitmap
希望可以幫你。
Android 通過軟引用實現圖片緩衝,防止記憶體溢出
public class BitmapCache {
static private BitmapCache cache;
/** 用於Chche內容的儲存 */
private Hashtable<Integer, MySoftRef> hashRefs;
/** 垃圾Reference的隊列(所引用的對象已經被回收,則將該引用存入隊列中) */
private ReferenceQueue<Bitmap> q;
/**
* 繼承SoftReference,使得每一個執行個體都具有可識別的標識。
*/
private class MySoftRef extends SoftReference<Bitmap> {
private Integer _key = 0;
public MySoftRef(Bitmap bmp, ReferenceQueue<Bitmap> q, int key) {
super(bmp, q);
_key = key;
}
}
private BitmapCache() {
hashRefs = new Hashtable<Integer, MySoftRef>();
q = new ReferenceQueue<Bitmap>();
}
/**
* 取得緩衝器執行個體
*/
public static BitmapCache getInstance() {
if (cache == null) {
cache = new BitmapCache();
}
return cache;
}
/**
* 以軟引用的方式對一個Bitmap對象的執行個體進行引用並儲存該引用
*/
private void addCacheBitmap(Bitmap bmp, Integer key) {
cleanCache();// 清除垃圾引用
MySoftRef ref = new MySoftRef(bmp, q, key);
hashRefs.put(key, ref);
}
/**
* 依據所指定的drawable下的圖片資源ID號(可以根據自己的需要從網路或本地path下擷取),重新擷取相應Bitmap對象的執行個體
*/
public Bitmap getBitmap(int resId, Context context) {
Bitmap bmp = null;
// 緩衝中是否有該Bitmap執行個體的軟引用,如果有,從軟引用中取得。
if (hashRefs.containsKey(resId)) {
MyS......餘下全文>>
Android小記讀取Bitmap 的幾種方式與最佳化記憶體溢出
查了很多資料終於找到了。現總結以下幾種讀取Bitmap的方法。1.以檔案流的方式,假設在sdcard下有 test.png圖片FileInputStream fis = new FileInputStream("/sdcard/test.png");Bitmap bitmap = BitmapFactory.decodeStream(fis);2. 以R檔案的方式,假設 res/drawable下有 test.jpg檔案Bitmap bitmap = BitmapFactory.decodeResource(this.getContext().getResources(), R.drawable.test);3.以ResourceStream的方式,但不用到R檔案。Bitmap.bitmap=BitmapFactory.decodeStream(getClass().getResourceAsStream(“/res/drawable/test.png”));BitmapFactory.Options options = new BitmapFactory.Options();options.inSampleSize = 2; //圖片寬高都為原來的二分之一,即圖片為原來的四分一//以上代碼可以最佳化記憶體溢出,但它只是改變圖片大小,並不能徹底解決記憶體溢出。