0

以下从图像视图中获取位图的过程在 Android 2.3 中不起作用,它在 Google nexus 中运行良好。有什么需要改变的地方吗?

ImageView thumbImage =(ImageView) findViewById(R.id.imageView1);

thumbImage.setDrawingCacheEnabled(true);

Bitmap pr = thumbImage.getDrawingCache();

我正在使用的实际过程。

    zoomImage.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        if (thumbImage.getDrawable() != null) {
                            try {

                                thumbImage.setDrawingCacheEnabled(true);
                                Bitmap pre = thumbImage.getDrawingCache();
                                if (zoomDialog == null)
                                    zoomDialog = new Dialog(EachProduct.this,
                                            R.style.ingredienttheme);
                                LayoutInflater lf = getLayoutInflater();
                                final View w = lf.inflate(R.layout.zoom_dialog,
                                        null);
                                zoomDialog.setContentView(w);
                                zoomDialog.getWindow().getAttributes().windowAnimations = R.style.Animation;
                                final ImageView z = (ImageView) zoomDialog
                                        .findViewById(R.id.imageView1);
                                ImageView close = (ImageView) zoomDialog
                                        .findViewById(R.id.imageView2);

                                Bitmap newimage = Bitmap.createScaledBitmap(pre,
                                        displayWidth - 25, displayWidth - 25, true);

                                /*
                                 * ByteArrayOutputStream stream = new
                                 * ByteArrayOutputStream();
                                 * newimage.compress(Bitmap.CompressFormat.PNG, 100,
                                 * stream); byte[] byteArray = stream.toByteArray();
                                 * Bitmap a=BitmapFactory.decodeByteArray(byteArray,
                                 * 0, byteArray.length, o2);
                                 */

                                z.setImageBitmap(newimage);
                                close.setOnClickListener(new OnClickListener() {
                                    @Override
                                    public void onClick(View arg0) {
                                        z.setImageDrawable(null);
                                        // z=null;
                                        zoomDialog.cancel();
                                    }
                                });
                                zoomDialog.show();
                            } catch (OutOfMemoryError e) {
                                // TODO: handle exception
                                System.out.println(e.toString());
                            }
                        }
                    }
                });

日志错误:

06-03 14:47:48.114: E/AndroidRuntime(14051): FATAL EXCEPTION: main
06-03 14:47:48.114: E/AndroidRuntime(14051): java.lang.NullPointerException
06-03 14:47:48.114: E/AndroidRuntime(14051):    at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:374)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at com.sparx.maternity.activities.EachProduct$9.onClick(EachProduct.java:510)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at android.view.View.performClick(View.java:2485)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at android.view.View$PerformClick.run(View.java:9089)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at android.os.Handler.handleCallback(Handler.java:587)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at android.os.Looper.loop(Looper.java:123)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at android.app.ActivityThread.main(ActivityThread.java:3806)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at java.lang.reflect.Method.invoke(Method.java:507)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-03 14:47:48.114: E/AndroidRuntime(14051):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

0

试试这样:

ImageView thumbImage =(ImageView) findViewById(R.id.imageView1);

thumbImage.setDrawingCacheEnabled(true);

Bitmap pr = Bitmap.createBitmap(thumbImage.getDrawingCache());

thumbImage.setDrawingCacheEnabled(false);

或者,如果它不起作用,请尝试thumbImage.buildDrawingCache()在之前调用thumbImage.getDrawingCache()

于 2013-06-03T09:23:19.660 回答