将框架布局保存为具有其subViews的位图,但将imageView(subView)赋予黑屏 [英] saving frame layout as bitmap with its subViews but imageView(subView) giving blackscreen

查看:87
本文介绍了将框架布局保存为具有其subViews的位图,但将imageView(subView)赋予黑屏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个框架布局. 它包括一个按钮. 在运行时,我单击按钮以从图库中选择图像.

I have a framelayout. it includes a button. At runtime I click the button to pick an image from the gallery.

我创建一个imageView,在imageView上的图库中设置所选图像,然后将imageView添加到framelayout中.

I create a imageView ,set the chosen image from the gallery on the imageView and add the imageView to framelayout.

然后我将整个视图(帧布局)另存为位图. 但是保存的位图仅显示按钮和imageView所在的黑屏.

then i save the entire view(framelayout) as a bitmap. But the saved bitmap only shows the button and a black screen where the imageView should be.

我尝试了很多方法,但是无法解决...

I tried a lot of ways but could not resolve it...

请帮助

public class MainActivity extends AppCompatActivity {

Button b;
RelativeLayout frame;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b = (Button) findViewById(R.id.b);
    frame = (RelativeLayout) findViewById(R.id.frame);

    b.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, 1);
        }
    });
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode){
        case 1 :
            Uri selectedImage = data.getData();
              try {
                bitmap = MediaStore.Images.Media.getBitmap(
                        MainActivity.this.getContentResolver(), selectedImage);
            } catch (IOException e) {
                e.printStackTrace();
            }





            ImageView i = new ImageView(MainActivity.this);
            i.setImageBitmap(bitmap);

            frame.addView(i);
            frame.invalidate();


           Bitmap bitmapFromView = Bitmap.createBitmap(frame.getWidth(),frame.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvass = new Canvas(bitmapFromView);
            frame.draw(canvass);
            saveBitmap(bitmapFromView);


            break;
    }

}


public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }

    MediaScannerConnection.scanFile(getApplicationContext(),
            new String[]{imagePath.getAbsolutePath()}, null,
            new MediaScannerConnection.OnScanCompletedListener() {

                @Override
                public void onScanCompleted(String path, Uri uri) {
                    // TODO Auto-generated method stub

                }
            });
}




 /*
 also tried this...not working

public static Bitmap loadBitmapFromView(View v) {
    Bitmap bitmap;
    v.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v.getDrawingCache());
    v.setDrawingCacheEnabled(false);
    return bitmap;
   }

    */

}

推荐答案

我做到了.

我正在onActivityResult()中顺序添加图像并进行保存;

I WAS ADDING THE IMAGE AND SAVING IT SEQUENTIALLY IN onActivityResult();

此:

        ImageView i = new ImageView(MainActivity.this);
        i.setImageBitmap(bitmap);
        frame.addView(i);
        frame.invalidate();

和这个:

        Bitmap bitmapFromView =   Bitmap.createBitmap(frame.getWidth(),frame.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvass = new Canvas(bitmapFromView);
        frame.draw(canvass);
        saveBitmap(bitmapFromView);

似乎添加的图像在设备上对我来说是可见的,但android内部没有对其进行处理.在那之前,我试图保存视图.

It seems that although the added image was visible to me on device but internally android had not processed it.And before that that could happen I was trying to save the view.

现在,我终于通过在onActivityResult()中添加imageView解决了它. 并将此视图保存在单独的按钮上.

Now I finally solved it by adding the the imageView in onActivityResult(); and saving this view on a separate button click.

当我启动一个工作线程时,我也获得了积极的结果,该工作线程在添加imageview之后将睡眠1秒钟. 然后,在工作线程的postExecute()中,将视图另存为位图.

I also got positive result when I fired a worker thread which would sleep for 1 second after adding the imageview. Then in postExecute() of the worker thread I saved the view as bitmap.

这篇关于将框架布局保存为具有其subViews的位图,但将imageView(subView)赋予黑屏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆