Android SDK 不会将文件加载为位图...我该如何解决这个问题?

发布于 2024-12-08 23:39:17 字数 2886 浏览 1 评论 0原文

我一直在阅读 Mario Zechner 所著的《Beginning Android Games》一书,他在 Google 代码中提供了一个示例。

http://code.google.com/p/beginning-android-games/source/browse/trunk/ch04-android-basics/src/com/badlogic/androidgames/BitmapTest.java

当我运行这个时,它只是关闭了我,并说主要活动已意外关闭。

当我运行 hello world 时,SDK 工作得很好,但我不知道从哪里开始。

Android 2.2 API Level 8 是我选择的设备,因为我的手机就是这样的。

这是我试图运行的代码。

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends Activity {
    class RenderView extends View {
        Bitmap bob565;
        Bitmap bob4444;
        Rect dst = new Rect();

        public RenderView(Context context) {
            super(context);

            try {
                AssetManager assetManager = context.getAssets();
                InputStream inputStream = assetManager.open("helmet.png");
                bob565 = BitmapFactory.decodeStream(inputStream);
                inputStream.close();
                Log.d("BitmapText",
                        "bobrgb888.png format: " + bob565.getConfig());

                inputStream = assetManager.open("helmet.png");
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_4444;
                bob4444 = BitmapFactory
                        .decodeStream(inputStream, null, options);
                inputStream.close();
                Log.d("BitmapText",
                        "bobargb8888.png format: " + bob4444.getConfig());

            } catch (IOException e) {
                // silently ignored, bad coder monkey, baaad!
            } finally {
                // we should really close our input streams here.
            }
        }

        protected void onDraw(Canvas canvas) {
            dst.set(50, 50, 350, 350);
            canvas.drawBitmap(bob565, null, dst, null);
            canvas.drawBitmap(bob4444, 100, 100, null);
            invalidate();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new RenderView(this));
    }
}

I have been reading the book called "Beginning Android Games" by Mario Zechner and he has an example that is linked here in Google code.

http://code.google.com/p/beginning-android-games/source/browse/trunk/ch04-android-basics/src/com/badlogic/androidgames/BitmapTest.java

When I ran this it just shutdown on me and said Main Activity has shutdown unexpectedly.

When I ran the hello world the SDK worked just fine, but I am not sure where to start.

Android 2.2 API Level 8 is the Device I have selected since that's what my phone has.

Here is the code I was trying to run.

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.io.IOException;
import java.io.InputStream;

public class MainActivity extends Activity {
    class RenderView extends View {
        Bitmap bob565;
        Bitmap bob4444;
        Rect dst = new Rect();

        public RenderView(Context context) {
            super(context);

            try {
                AssetManager assetManager = context.getAssets();
                InputStream inputStream = assetManager.open("helmet.png");
                bob565 = BitmapFactory.decodeStream(inputStream);
                inputStream.close();
                Log.d("BitmapText",
                        "bobrgb888.png format: " + bob565.getConfig());

                inputStream = assetManager.open("helmet.png");
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inPreferredConfig = Bitmap.Config.ARGB_4444;
                bob4444 = BitmapFactory
                        .decodeStream(inputStream, null, options);
                inputStream.close();
                Log.d("BitmapText",
                        "bobargb8888.png format: " + bob4444.getConfig());

            } catch (IOException e) {
                // silently ignored, bad coder monkey, baaad!
            } finally {
                // we should really close our input streams here.
            }
        }

        protected void onDraw(Canvas canvas) {
            dst.set(50, 50, 350, 350);
            canvas.drawBitmap(bob565, null, dst, null);
            canvas.drawBitmap(bob4444, 100, 100, null);
            invalidate();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(new RenderView(this));
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

酒绊 2024-12-15 23:39:17

哦天哪,请将helmet.png图像放入项目assets目录中......

这段代码完全运行正常!唯一可能存在的问题是 helmet.png 找不到

Oh dear, please put helmet.png image into project assets directory....

This code is totally running fine! The only problem can exist is that helmet.png not found

小女人ら 2024-12-15 23:39:17

AssetManager 方法不起作用。所以我尝试了以下方法,这对我来说确实有效。

public Bitmap getImage(String location) {
        InputStream is = RenderView.class.getResourceAsStream(location);
        return BitmapFactory.decodeStream(is);
}

RenderView 只是当前类...因此,如果您在 Main.java 下编写此代码,您将编写:

Main.class.getResourcesAsStream();

location 只是文件的路径。例如,如果您在 images 文件夹下有一个名为 ball.jpg 的图像,您只需编写:

String location = "images/ball.png";

The AssetManager way did not work. So I tried the following method and this did work for me.

public Bitmap getImage(String location) {
        InputStream is = RenderView.class.getResourceAsStream(location);
        return BitmapFactory.decodeStream(is);
}

RenderView is just the current class... so if your writing this code under Main.java you would write:

Main.class.getResourcesAsStream();

location is just the path to the file. For example if you have a image called ball.jpg under images folder you would just write:

String location = "images/ball.png";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文