3

我的应用程序崩溃了,我似乎无法解决。

我像这样从资源中获得位图。

Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.map_distance_tag);

现在,当从该位图创建位图时,这似乎在极少数设备上存在问题。例如...

Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());

NullPointerException当它创建类的Bitmap第 468行时,我得到Bitmap了一个,我已经查看了它,当它从其源位图设置新位图的密度比例时。

// The new bitmap was created from a known bitmap source so assume that
467        // they use the same density scale
468        bitmap.setDensityScale(source.getDensityScale());

现在我似乎无法重新创建问题,但是当在原始位图上调用 getWidth() 时我没有得到异常的事实告诉我源不为空。

这是整个堆栈跟踪。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.my.app/com.my.app.recording.TrackingActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.graphics.Bitmap.createBitmap(Bitmap.java:468)
at com.my.app.maps.MapFragment.createTabOverlay(MapFragment.java:663)
at com.my.app.maps.MapFragment.addMarkersForToolType(MapFragment.java:474)
at com.my.app.maps.MapFragment.onActivityCreated(MapFragment.java:405)
at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
at com.my.app.recording.TrackingActivity.onStart(TrackingActivity.java:272)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
at android.app.Activity.performStart(Activity.java:3781)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2636)
... 11 more

我的想法

不包括针对不同屏幕密度的不同位图可能与我有关吗?提供的所有位图都用于 mdpi 屏幕。

我注意到还有一个 BackStackRecord。当用户按下时,这会崩溃吗?

提前致谢

4

5 回答 5

4

您指向的来源Bitmap.java:468来自 Android 1.5,可能不是您的问题。

v1.5 位图.java:468

bitmap.setDensityScale(source.getDensityScale());

v1.6-v2.2 Bitmap.java:468

Bitmap bm = nativeCreate(null, 0, width, width, height, config.nativeInt, true);

任何比 2.2 更新的版本中的第 468 行要么是 javadoc 块的一部分,要么是不相关的函数,所以我猜你正在查看 1.6-2.2 版本,而不是你试图从中排除故障的那个。

我看到那条线上唯一可能真正以 null 结束的是config. 在您的代码中,您使用getConfig()从当前位图对象中获取它。它的文档显示:

public final Bitmap.Config getConfig()

在 API 级别 1 中添加

如果位图的内部配置是其中一种公共格式,则返回该配置,否则返回 null。

所以,它可能没有找到“公共”格式,并返回 null,它传递给createBitmap(),并炸毁你的代码。

您可以始终使用静态配置,例如Bitmap.Config.ARGB_8888,或者在getConfig()上一行执行并对其进行空检查。如果为 null,则转到刚才提到的后备配置。

于 2013-01-29T02:10:12.343 回答
1

你可以这样做:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.icon);
Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 600, 400, true);

ByteArrayOutputStream bos = new ByteArrayOutputStream();

// CompressFormat set up to JPG, you can change to PNG
bmpCompressed.compress(CompressFormat.JPEG, 90, bos);
imageByteArray = bos.toByteArray();
于 2013-01-21T11:45:02.973 回答
1

yourSelectedImageBitmap = BitmapFactory.decodeFile(filePath, options);

    if (yourSelectedImageBitmap != null) {
        // ----------resize for set in imageView------------
        int width = yourSelectedImageBitmap.getWidth();
        int height = yourSelectedImageBitmap.getHeight();
        int newWidth = 140;
        int newHeight = 140;

        // calculate the scale - in this case = 0.4f
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix = new Matrix();
        // resize the bit map
        matrix.postScale(scaleWidth, scaleHeight);
        if (exifOrientation.equals("6")) {
            matrix.postRotate(90);
        }

        Bitmap resizedBitmap = Bitmap.createBitmap(yourSelectedImageBitmap,
                0, 0, width, height, matrix, true);
        image.setImageBitmap(resizedBitmap);

        // Toast.makeText(getApplicationContext(),
        // "in the select image loop", 2000).show();

        // ----------resize for upload------------
        int width1 = yourSelectedImageBitmap.getWidth();
        int height1 = yourSelectedImageBitmap.getHeight();

您可以通过这种方式尝试一下。从文件源或相机获取图像后。

然后通过这样做。

Matrix matrix1 = new Matrix();
        // resize the bit map
        matrix.postScale(width1, height1);
        if (exifOrientation.equals("6")) {
            matrix1.postRotate(90);
        }
        Bitmap resizedBitmap1 = Bitmap.createBitmap(
                yourSelectedImageBitmap, 0, 0, width1, height1, matrix1,
                true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        resizedBitmap1.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] image_bytes = baos.toByteArray();

        try {
            baos.flush();
            baos.close();

        } catch (IOException e) {
            e.printStackTrace();
            Log.e("~~~~~~~~IOException~~~~~~~~~eeeeee~~~~",
                    "~~~~~~IOException~~~~~~~~~eeeee~~~~" + e.getMessage());
        }
        image_string = Base64.encodeToString(image_bytes, 0);

希望对你有帮助

于 2013-01-21T10:57:53.117 回答
0

我有同样的问题,这让我发疯。我最终通过改变解决了这个问题

Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig());

Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), Bitmap.Config.ARGB_8888);
于 2014-02-12T15:56:54.327 回答
0

我认为这会奏效

 BitmapFactory.Options opt = new BitmapFactory.Options();
 opt.inMutable = true;  //you have to set bitmap to mutable
 bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.map_distance_tag,opt);
 Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), Bitmap.Config.ARGB_8888);

 //if you want to copy the bmp1 to bmOverlay
 bmOverlay=Bitmap.createScaledBitmap(bmp1, bmp1.getWidth, bmp1.getHeight, false);

         OR

//if did not work replace bmOverlay to this:
 bmOverlay = Bitmap.createBitmap(bmp1);
于 2013-01-25T02:40:37.903 回答