editText.buildDrawingCache上的位图大小超过32位

时间:2016-02-27 12:53:16

标签: java android android-studio

抱歉英语不好。我想制作一个图像并在片段中显示,而当我使用editText中的OnLongClickListener()时,片段会弹出。

但我得到了这样的输出:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.project.testingtexttoimage, PID: 23849
java.lang.IllegalArgumentException: bitmap size exceeds 32bits 
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
at android.graphics.Bitmap.createBitmap(Bitmap.java:769)
at android.view.View.buildDrawingCache(View.java:13649)
at android.view.View.buildDrawingCache(View.java:13563)
at com.project.testingtexttoimage.MainActivity$1.onLongClick(MainActivity.java:31)
at android.view.View.performLongClick(View.java:4478)
at android.widget.TextView.performLongClick(TextView.java:8652)
at android.view.View$CheckForLongPress.run(View.java:18452)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)

我的MainActivity是这样的:

findViewById(R.id.buttonTest).setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {

        // make image from edit text
        findViewById(R.id.editTextTest).measure(
                View.MeasureSpec.makeMeasureSpec(findViewById(R.id.editTextTest).getLayoutParams().width, View.MeasureSpec.EXACTLY),
                View.MeasureSpec.makeMeasureSpec(findViewById(R.id.editTextTest).getLayoutParams().height, View.MeasureSpec.EXACTLY));
        findViewById(R.id.editTextTest).layout(0, 0, findViewById(R.id.editTextTest).getMeasuredWidth(), findViewById(R.id.editTextTest).getMeasuredHeight());
        findViewById(R.id.editTextTest).setDrawingCacheEnabled(true);
        findViewById(R.id.editTextTest).buildDrawingCache();
        Bitmap bmp = Bitmap.createBitmap(findViewById(R.id.editTextTest).getDrawingCache());
        findViewById(R.id.editTextTest).setDrawingCacheEnabled(false);


        // convert bitmap to byte array
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
        byte[] byteArrayBitmap = stream.toByteArray();

        // show fragment
        SupportDialogFragment fragment
                = SupportDialogFragment.newInstance(
                3 + 1,
                1 + 2,
                false,
                false,
                true,
                false,
                "Keep Trying Make Something Awesome !!!",
                byteArrayBitmap

        );
        fragment.show(getSupportFragmentManager(), "blur_sample");

        return true;
    }
});
顺便说一句,我使用BlurFragment弹出.. 在你之前帮助你。

1 个答案:

答案 0 :(得分:0)

解决所有这一切......

findViewById(R.id.editTextTest).measure(此代码无法用于查找高度和宽度,因此我更改高度和宽度,如下面的代码,以及它的工作......难以置信......非常好......

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.editTextTest).setFocusable(false);

        findViewById(R.id.buttonTest).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {

                // get view
                View view = findViewById(R.id.editTextTest);
                view.setDrawingCacheEnabled(true);

                // get height and width from scroll view
                ScrollView scrollView = (ScrollView)findViewById(R.id.scroolView);
                int totalHeight = scrollView.getChildAt(0).getHeight();
                int totalWidth = scrollView.getChildAt(0).getWidth();

                // set view
                view.layout(0,0,totalWidth,totalHeight);

                // build drawing cache
                view.buildDrawingCache(true);

                // bitmap storage
                Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());

                // clear cache
                view.setDrawingCacheEnabled(false);

                // convert bitmap to byte array
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArrayBitmap = stream.toByteArray();

                // show fragment
                SupportDialogFragment fragment
                        = SupportDialogFragment.newInstance(
                        3 + 1,
                        1 + 2,
                        false,
                        false,
                        true,
                        false,
                        "Keep Trying Make Something Awesome !!!",
                        byteArrayBitmap

                );
                fragment.show(getSupportFragmentManager(), "blur_sample");

                return true;
            }
        });


    }