【问题标题】:Android Emulator only able to upload 5 JPEG imagesAndroid Emulator 只能上传 5 张 JPEG 图片
【发布时间】:2013-04-22 11:14:45
【问题描述】:

我能够在 android Emulator 上运行 5 张 JPEG 图像。如果我上传超过 5 张图片,模拟器将无法启动活动。

请帮忙...

这是我的 ImageAdapter 代码。当我单击图像 mThumbIds(即 150,150)时,它会启动另一个活动。仅当我的数组中有 5 个 JPEG 图像时才会发生这种情况,如果我有超过 5 个 JPEG 图像,程序将崩溃..

我也尝试添加 png 图像,但同样的问题。

这两天我一直卡在这个问题上..仍然没有找到任何解决方案

 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;


    }



}

我的 Log cat 显示如下

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)

【问题讨论】:

  • 哪个模拟器,哪个版本?你收到什么错误?请更具体
  • 你能告诉我们你的图像适配器代码吗? (如果你使用。如果没有,你怎么能加载图像)

标签: android android-layout android-intent


【解决方案1】:

我不确定这些图片的大小,但我怀疑它们比 150x150 大得多?

您可以通过以下 2 个函数以更接近 150dp 图像视图的较低分辨率加载它们。

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));

【讨论】:

  • K 这行得通,但它产生了另一个问题,它给了我空白屏幕。图像仍然打开(假设它的位置有 150,150 张图片)。换句话说,拇指在空白屏幕后面。
  • 我可以向你保证以上功能不会导致黑屏,它是生产代码。
【解决方案2】:

您遇到内存不足错误。这可能会通过调整内存选项中的模拟器 RAM 在模拟器中修复。您可能还想尝试增加 VM 堆。

还可以帮助您降低资源图像的分辨率。您可以按照不同答案的建议以编程方式执行此操作,或者在您的资源中仅包含一些较低分辨率的图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-28
    • 2016-11-22
    • 2014-12-19
    • 1970-01-01
    • 1970-01-01
    • 2017-05-21
    • 2017-06-15
    • 2019-11-20
    相关资源
    最近更新 更多