2013-04-28 35 views
1

我能夠在Android模擬器上運行5個JPEG圖像。如果我上傳超過5張圖片,模擬器無法啓動活動。Android模擬器只能夠上傳5個JPEG圖像

請幫助...

這是我的ImageAdapter代碼..當我在圖像mThumbIds點擊(這是150,150),它開始另一個活動。這隻發生在我的數組中有5張JPEG圖像時,如果我有超過5張JPEG圖像,程序會崩潰。

我也嘗試添加png圖像,但同樣的問題。

我一直停留在此的最後兩個days..and仍然還沒有找到任何解決辦法

package com.example.first; 

import android.content.Context; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.BaseAdapter; 
import android.widget.GridView; 
import android.widget.ImageView; 

public class ImageAdapter extends BaseAdapter{ 

    private Context mContext; 
    public Integer[] mThumbIds = { 
      R.drawable.h18, R.drawable.h17, R.drawable.h16, R.drawable.h15, 



    }; 
    public ImageAdapter(Context c) { 
     mContext = c; 
    } 

    @Override 
    public int getCount() { 
     return mThumbIds.length; 
    } 

    @Override 
    public Object getItem(int position) { 

     return mThumbIds[position]; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageview; 
     if(convertView == null){ 
      imageview = new ImageView(mContext); 
      imageview.setLayoutParams(new GridView.LayoutParams(150,150)); 
      imageview.setScaleType(ImageView.ScaleType.CENTER_CROP); 
      imageview.setPadding(8, 8, 8, 8); 
     }else{ 
      imageview = (ImageView) convertView; 
     } 
     imageview.setImageResource(mThumbIds[position]); 
     return imageview; 


    } 



} 

我的日誌貓顯示以下

04-28 21:54:33.645: E/AndroidRuntime(1213): FATAL EXCEPTION: main 
04-28 21:54:33.645: E/AndroidRuntime(1213): java.lang.OutOfMemoryError 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:502) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:355) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:785) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.content.res.Resources.loadDrawable(Resources.java:1965) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.content.res.Resources.getDrawable(Resources.java:660) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.ImageView.resolveUri(ImageView.java:616) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.ImageView.setImageResource(ImageView.java:349) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at com.example.first.ImageAdapter.getView(ImageAdapter.java:50) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.AbsListView.obtainView(AbsListView.java:2159) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.GridView.makeAndAddView(GridView.java:1341) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.GridView.makeRow(GridView.java:341) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.GridView.fillDown(GridView.java:283) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.GridView.fillFromTop(GridView.java:417) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.GridView.layoutChildren(GridView.java:1229) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.AbsListView.onLayout(AbsListView.java:1994) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.View.layout(View.java:14008) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.ViewGroup.layout(ViewGroup.java:4373) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.View.layout(View.java:14008) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.ViewGroup.layout(ViewGroup.java:4373) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.View.layout(View.java:14008) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.ViewGroup.layout(ViewGroup.java:4373) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1663) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1521) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1434) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.View.layout(View.java:14008) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.ViewGroup.layout(ViewGroup.java:4373) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.widget.FrameLayout.onLayout(FrameLayout.java:448) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.View.layout(View.java:14008) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.ViewGroup.layout(ViewGroup.java:4373) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1892) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1711) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.Choreographer.doCallbacks(Choreographer.java:562) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.Choreographer.doFrame(Choreographer.java:532) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.os.Handler.handleCallback(Handler.java:725) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.os.Looper.loop(Looper.java:137) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
04-28 21:54:33.645: E/AndroidRuntime(1213):  at dalvik.system.NativeStart.main(Native Method) 
+0

哪個模擬器,哪個版本?你收到什麼錯誤?請更具體地 – Addys 2013-04-28 21:01:59

+0

你能告訴我們你的圖像適配器代碼嗎? (如果你使用的話,如果沒有,你怎麼能載入圖像) – alicanbatur 2013-04-28 21:13:05

回答

0

我不知道什麼樣的規模這些圖像,但我懷疑他們比150x150大得多?

您可以使用更低的分辨率將它們加載到更接近您的150dp圖像瀏覽體與以下2個功能。

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { 
    // Raw height and width of image 
    final int height = options.outHeight; 
    final int width = options.outWidth; 
    int inSampleSize = 1; 

    if(reqHeight + reqWidth == 0){ 
     Utils.logw("Bitmap decoded at full res due to width and height being zero."); 
     return inSampleSize; 
    } 
    else if (height > reqHeight || width > reqWidth) { 

     // Calculate ratios of height and width to requested height and width 
     final int heightRatio = Math.round((float) height/(float) reqHeight); 
     final int widthRatio = Math.round((float) width/(float) reqWidth); 

     // Choose the smallest ratio as inSampleSize value, this will guarantee 
     // a final image with both dimensions larger than or equal to the 
     // requested height and width. 
     inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; 
    } 

    return inSampleSize; 
} 

public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) { 
    // First decode with inJustDecodeBounds=true to check dimensions 
    final BitmapFactory.Options options = new BitmapFactory.Options(); 
    options.inJustDecodeBounds = true; 
    BitmapFactory.decodeResource(res, resId, options); 

    // Calculate inSampleSize 
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

    // Decode bitmap with inSampleSize set 
    options.inJustDecodeBounds = false; 
    return BitmapFactory.decodeResource(res, resId, options); 
} 

並更換您的通話

imageview.setImageResource(mThumbIds[position]); 

imageview.setImageBitmap(decodeSampledBitmapFromResource(mThumbIds[position], 150, 150)); 
+0

K這個工作,但它造成了另一個問題,它給了我空白屏幕。圖片仍然打開(假設150,150圖片在它的位置)。換句話說,拇指在黑屏後面。 – 2013-04-29 00:46:50

+0

我可以保證你的上述功能不會導致黑屏,它是生產代碼。 – 2013-04-29 01:02:05

0

你得到一個內存不足的錯誤。這可能會通過調整內存選項中的仿真器RAM在仿真器中解決。你也可能想要增加VM堆。

還可以幫助你的是減少資源圖像的分辨率。您可以按照其他答案的建議以編程方式執行此操作,或者只需在資源中使用較低分辨率的圖像。

相關問題