从一个活动传递到另一个位图图像 [英] pass a bitmap image from one activity to another

查看:100
本文介绍了从一个活动传递到另一个位图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我在那里,只要我选择一个图像时,图像应发送到所选择的图像将被设置为background.However新的活动显示NO.OF图像从画廊,我能够从画廊获得图像,但是只要我选择一个事先申请crashes.Thanks

活动-1(将图像显示在画廊和所选择的图像发送到新的活动)

 公共类廊延伸活动{

私有静态诠释RESULT_LOAD_IMAGE = 1;

    / **第一次创建活动时调用。 * /
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.gallery);

        按钮buttonLoadImage =(按钮)findViewById(R.id.buttonLoadPicture);


        buttonLoadImage.setOnClickListener(新View.OnClickListener(){

            公共无效的onClick(查看为arg0){

                意图I =新的意图(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(ⅰ,RESULT_LOAD_IMAGE);
            }
        });
    }



    保护无效onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,因此code,数据);

        如果(要求code == RESULT_LOAD_IMAGE和放大器;&安培;结果code == RESULT_OK和放大器;&安培;!NULL =数据){






            乌里contentUri = data.getData();
            的String []凸出= {MediaStore.Images.Media.DATA};
            光标光标= managedQuery(contentUri,凸出,NULL,NULL,NULL);
            INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            字符串tmppath = cursor.getString(Column_Index中);
            位图croppedImage = BitmapFactory.de codeFILE(tmppath);


            //位图croppedImage = BitmapFactory.de codeFILE(croppedImage);
            意向意图=新的意图(Gallery.this,GesturesActivity.class);
            intent.putExtra(BMP,croppedImage);
            startActivity(意向);

            Log.v(发送图像,发送图像);


        }


    }
}
 

活动-1(XML)

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT
    机器人:背景=@可绘制/背景
    >
    < ImageView的
            机器人:ID =@ + ID / imgView
            机器人:layout_width =FILL_PARENT
            机器人:layout_weight =1机器人:layout_height =WRAP_CONTENT>< / ImageView的>
    <按钮
            机器人:layout_height =WRAP_CONTENT
            机器人:文本=加载图片
            机器人:layout_width =WRAP_CONTENT
            机器人:ID =@ + ID / buttonLoadPicture
            机器人:layout_weight =0
            机器人:layout_gravity =中心>< /按钮>
< / LinearLayout中>
 

活性-2(其中,所选择的图像应该被设置为在屏幕的背景图像的活动)

 公共类GesturesActivity延伸活动{


        私人最终诠释MENU_CAMERA = Menu.FIRST;


        公共无效的onCreate(包savedInstanceState){
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);


            BMP位=(位图)this.getIntent()getParcelableExtra(BMP)。
            BitmapDrawable背景=新BitmapDrawable(BMP);
            getWindow()setBackgroundDrawable(背景)。 //背景屏幕的图像



            点阵位图= BitmapFactory.de codeResource(getResources(),R.drawable.advert);
            查看查看=新SandboxView(本,位图);

            的setContentView(视图);
        }



        公共布尔prepareOptionsMenu(功能菜单)在{
            menu.clear();

                menu.add(0,11,0,拍摄快照);

                    返回super.on prepareOptionsMenu(菜单);
        }


        公共布尔onOptionsItemSelected(菜单项项){

            返回super.onOptionsItemSelected(项目);
        }



    }
 

解决方案

有3解决方案来解决这个问题。

1)首先将图像转换成字节数组,然后传递到意向并在接下来的活动得到捆绑字节数组转换为图像(位图),并设置成ImageView的。

位图转换为字节数组: -

  BMP位= BitmapFactory.de codeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream流=新ByteArrayOutputStream();
bmp.com preSS(Bitmap.Com pressFormat.PNG,100,流);
字节[]的字节数组= stream.toByteArray();
 

通字节数组的意图: -

 意向意图=新的意图(这一点,NextActivity.class);
intent.putExtra(图片报,字节);
startActivity(意向);
 

获取字节数组的捆绑,并转换为位图图像: -

 捆绑额外= getIntent()getExtras()。
字节[]的字节数组= extras.getByteArray(图片);

BMP位= BitmapFactory.de codeByteArray(字节数组,0,byteArray.length);
ImageView的形象=(ImageView的)findViewById(R.id.imageView1);

image.setImageBitmap(BMP);
 

2)第一次保存的图像到SD卡,并在接下来的活动设置此图片为ImageView的。

3)合格的位图到意向并得到位从包下一个活动,但问题是,如果你的位图/影像尺寸大当时的形象是不是在明年的活动加载。

In my app i am displaying no.of images from gallery from where as soon as I select one image , the image should be sent to the new activity where the selected image will be set as background.However, I am able to get the images from gallery but as soon as I select one the application crashes.Thanks in advance

Activity-1(The images are shown in gallery and the selected image is sent to new activity)

public class Gallery extends Activity {

private static int RESULT_LOAD_IMAGE = 1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.gallery);

        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);


        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {

                Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }



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

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {






            Uri contentUri = data.getData();          
            String[] proj = { MediaStore.Images.Media.DATA };         
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);         
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);         
            cursor.moveToFirst();         
            String tmppath = cursor.getString(column_index);           
            Bitmap croppedImage = BitmapFactory.decodeFile(tmppath);


            // Bitmap croppedImage = BitmapFactory.decodeFile(croppedImage);
            Intent intent = new Intent(Gallery.this,GesturesActivity.class);
            intent.putExtra("bmp",croppedImage);
            startActivity(intent);

            Log.v("sending image","sending image");


        }


    }
}

Activity-1(XML)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/background"
    >
    <ImageView
            android:id="@+id/imgView"
            android:layout_width="fill_parent"
            android:layout_weight="1" android:layout_height="wrap_content"></ImageView>
    <Button 
            android:layout_height="wrap_content" 
            android:text="Load Picture" 
            android:layout_width="wrap_content" 
            android:id="@+id/buttonLoadPicture" 
            android:layout_weight="0" 
            android:layout_gravity="center"></Button>
</LinearLayout>

Activity-2(The activity where the selected image should be set as background image of the screen)

  public class GesturesActivity extends Activity {


        private final int MENU_CAMERA = Menu.FIRST;


        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);


            Bitmap bmp = (Bitmap)this.getIntent().getParcelableExtra("bmp");
            BitmapDrawable background = new BitmapDrawable(bmp);
            getWindow().setBackgroundDrawable(background);  //background image of the screen



            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.advert);
            View view = new SandboxView(this, bitmap);

            setContentView(view);
        }



        public boolean onPrepareOptionsMenu(Menu menu) {
            menu.clear();

                menu.add(0, 11, 0, "Take Snapshot");

                    return super.onPrepareOptionsMenu(menu);
        }


        public boolean onOptionsItemSelected(MenuItem item) {

            return super.onOptionsItemSelected(item);
        }



    }

解决方案

There are 3 Solutions to solve this issue.

1) First Convert Image into Byte Array and then pass into Intent and in next activity get byte array from Bundle and Convert into Image(Bitmap) and set into ImageView.

Convert Bitmap to Byte Array:-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Pass byte array into intent:-

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

Get Byte Array from Bundle and Convert into Bitmap Image:-

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

2) First Save image into SDCard and in next activity set this image into ImageView.

3) Pass Bitmap into Intent and get bitmap in next activity from bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.

这篇关于从一个活动传递到另一个位图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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