0

我知道这个问题被问了很多次,但没有一个答案真的对我有帮助。我正在尝试从图库中挑选一张图片,然后将整个视图转换为位图(拍摄快照)。它在大多数情况下都可以正常工作,但有时它显示运行时错误并使应用程序崩溃。我不明白为什么它会偶尔显示。

以下是我将视图转换为位图的 java 片段,它位于 onCreate() 内部,

save.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i(TAG, "Saving...");
                // bitmap

                ContentValues cv = new ContentValues();
                TextView name = (TextView) findViewById(R.id.name);
                cv.put("victimname", name.getText().toString());

                TextView gender = (TextView) findViewById(R.id.gender);
                if (gender.getText().equals("MALE")) {
                    cv.put("gender", 1);
                } else {
                    cv.put("gender", 0);

                }

                Button save = (Button) findViewById(R.id.save);
                save.setVisibility(View.INVISIBLE);
                ImageView bckground = (ImageView) findViewById(R.id.imageView1);
                bckground.setImageResource(R.drawable.photocropscreenclearout);

                View content = findViewById(R.id.rootView);
                content.setDrawingCacheEnabled(true);
                Bitmap bitmap = content.getDrawingCache();
                // actual width of the image (img is a Bitmap object)
                int width = bitmap.getWidth();
                int height = bitmap.getHeight();

                // new width / height
                int newWidth = width / 2;
                int newHeight = height / 2;

                // calculate the scale
                float scaleWidth = (float) newWidth / width;
                float scaleHeight = (float) newHeight / height;

                // create a matrix for the manipulation
                Matrix matrix = new Matrix();

                // resize the bit map
                matrix.postScale(scaleWidth, scaleHeight);

                // recreate the new Bitmap and set it back
                bitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height,
                        matrix, true);

                Bitmap mb = bitmap.copy(Bitmap.Config.ARGB_8888, true);
                Canvas c = new Canvas(mb);

                // get the int for the colour which needs to be removed
                Paint p = new Paint();
                p.setARGB(255, 255, 255, 255); // ARGB for the color, in this
                                                // case red
                int removeColor = p.getColor(); // store this color's int for
                                                // later use
                p.setAlpha(0);

                p.setXfermode(new AvoidXfermode(removeColor, 30,
                        AvoidXfermode.Mode.TARGET));

                // draw transparent on the "brown" pixels
                c.drawPaint(p);


                ByteArrayOutputStream out = new ByteArrayOutputStream();
                // bitmap.compress(Bitmap.CompressFormat.PNG, 100,out);
                mb.compress(Bitmap.CompressFormat.PNG, 60, out);


                cv.put("imageData", out.toByteArray());
                DailiesCursor mySQLiteAdapter = new DailiesCursor(
                        getApplicationContext());
                mySQLiteAdapter.openDataBase();
                mySQLiteAdapter.insert("maindata", null, cv);

                Cursor contentRead = mySQLiteAdapter.getLast();
                startManagingCursor(contentRead);
                contentRead.moveToFirst();

                int id = 0;
                while (contentRead.isAfterLast() == false) {
                    id = contentRead.getInt(0);
                    contentRead.moveToNext();
                }
                contentRead.close();
                mySQLiteAdapter.close();

                Log.i(TAG, "Start Game!");
                // SELECT last_insert_rowid();
                content.setDrawingCacheEnabled(false);
                final Intent myIntent = new Intent(getApplicationContext(),
                        MainGame.class);
                myIntent.putExtra("_id", Integer.toString(id));
                startActivity(myIntent);
                finish();

            }

        });

编辑1: 以下是xml文件,

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" 
        android:id="@+id/rootView">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:scaleType="fitXY"
        android:layout_weight="1"
        android:src="@drawable/photocropscreen" />

    <Button
        android:id="@+id/back"
       android:layout_width="100dp"
        android:layout_height="90dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"

        android:background="@null"
        android:text="BACK"
        android:textColor="#ffffff"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Button
        android:id="@+id/save"
        android:layout_width="100dp"
        android:layout_height="90dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"

          android:background="@null"
        android:text="SAVE"
          android:textColor="#ffffff"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <Button
        android:id="@+id/name"
       android:layout_width="100dp"
        android:layout_height="90dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentBottom="true"

        android:layout_marginBottom="50dp"
        android:text="NAME"
          android:textColor="#ffffff"
        android:background="@null"
        android:textAppearance="?android:attr/textAppearanceMedium" />


    <Button
        android:id="@+id/gender"
        android:layout_width="100dp"
        android:layout_height="90dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentBottom="true"

        android:layout_marginBottom="50dp"
        android:text="MALE"
          android:textColor="#ffffff"
        android:background="@null"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    </RelativeLayout>
4

0 回答 0