在android中建立bitmap避免記憶體不足的方法,androidbitmap

來源:互聯網
上載者:User

在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; //圖片寬高都為原來的二分之一,即圖片為原來的四分一//以上代碼可以最佳化記憶體溢出,但它只是改變圖片大小,並不能徹底解決記憶體溢出。
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.