导入图像并传递给另一个活动

时间:2013-12-28 11:42:20

标签: android image bitmap android-activity

我差点检查并在这里尝试了所有问题。但我无法将我从画廊中挑选的图像传递给另一个活动。这是onClickListener()

logoview=(ImageView)findViewById(R.id.logoview);
    logoview.setOnClickListener(new OnClickListener() 
    {            

    @Override
    public void onClick(View arg0) {

    openGallerylogo();
        }}); 

这是openGallery意图:

private void openGallerylogo() {
    // TODO Auto-generated method stub
    Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, 1);
}

这是我的onActivityResult()

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

    if (requestCode == 1) 
    {
        if (data != null && resultcode == RESULT_OK) 
        {              

            Uri selectedImage = data.getData();

            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String filePath = cursor.getString(columnIndex);
            cursor.close();
            if(bmp1 != null && !bmp1.isRecycled())
            {
                bmp1 = null;                
            }

            ImageView logoview = (ImageView) findViewById(R.id.logoview);
            logoview.setImageBitmap(BitmapFactory.decodeFile(filePath));
            bmp1 = BitmapFactory.decodeFile(filePath);

            if (count.equals("logo")){
                logoview.setBackgroundResource(0);
                logoview.setImageBitmap(bmp1);              
            }
            else {

                Log.d("Status:", "Photopicker canceled");    
            }}} 

此代码可以;点击ImageView,转到图库,选择图片并将其设置在ImageView中的同一MainActivity。点击按钮移动到下一个活动后,我想看到此图片是在ImageView中的另一个SecondActivity上设置的。谢谢。

2 个答案:

答案 0 :(得分:2)

如果您想要更改活动,请在下面写下代码。

发件人活动 Activity_one.java

Intent newdata = new Intent(Activity_one.this, Activity_Two.class);
newdata.putExtra("picture_path", filePath);
startActivity(newdata);

接收者活动 Activity_two.java

String pre_img_path= getIntent().getStringExtra("picture_path");
ImageView logoview = (ImageView) findViewById(R.id.logoview);
logoview.setImageBitmap(BitmapFactory.decodeFile(pre_img_path));

答案 1 :(得分:1)

将图像设置为ImageView后。这样做:

Bitmap myBitmap = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] bytepicture = baos.toByteArray();

Intent newdata = new Intent(MainMenu.this, YourNewActivity.class);
newdata.putExtra("picture", bytepicture);
startActivity(newdata);

并在onCreate()上的所需活动中检索为:

byte[] byteArray = extras.getByteArray("picture");
Bitmap image_bmp = BitmapFactory.decodeByteArray(byteArray, 0,
                byteArray.length);
yourimageview.setImageBitmap(image_bmp );