无法使用intent将从RelativeLayout创建的图像传递给新活动

时间:2016-03-29 01:52:43

标签: android android-layout android-intent

我正在尝试创建一个meme生成器应用程序。为此我将包含图像和几个文本视图的相对布局转换为位图。然后我将该图像传递给新活动。但在第二项活动中没有任何内容。它的空白。你能明白为什么吗?

我很感激你能提供的任何帮助。我一直在努力解决这个问题。

提前谢谢。

以下是代码:

DetailsActivity.java(包含相对布局的第一个活动包含一个图像和一些我转换为图像的TextViews)

package com.jobaka.www.jobaka;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.HashMap;

/**
 * Created by Psp on 2016-03-03.
 */
public class DetailsActivity extends AppCompatActivity {

    EditText etTop, etBottom;
    TextView tvTop, tvBottom;
    Button bCreate;
    String imgstring;

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

        Intent intent = getIntent();
        int id = intent.getIntExtra("id", -1);
        if(id != -1){
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), id);
            ImageView imageView = (ImageView) findViewById(R.id.image);
            imageView.setImageBitmap(bitmap);
        }else{

        }

        //imageUri = Uri.parse(extras.getString("imageUri"));

        etTop = (EditText) findViewById(R.id.etTop);
        tvTop = (TextView) findViewById(R.id.tvTop);
        etTop.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable s) {
                tvTop.setText(s.toString());
            }

            public void beforeTextChanged(CharSequence s, int start,int count, int after) {}

            public void onTextChanged(CharSequence s, int start,int before, int count) {}
        });

        etBottom = (EditText) findViewById(R.id.etBottom);
        tvBottom = (TextView) findViewById(R.id.tvBottom);
        etBottom.addTextChangedListener(new TextWatcher() {
            public void afterTextChanged(Editable sb) {
                tvBottom.setText(sb.toString());
            }

            public void beforeTextChanged(CharSequence sb, int start,int count, int after) {}

            public void onTextChanged(CharSequence sb, int start,int before, int count) {}
        });

        bCreate = (Button) findViewById(R.id.bCreate);
        bCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                RelativeLayout v = (RelativeLayout)findViewById(R.id.iFrame);
                Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
                        Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                view.draw(canvas);

                HashMap<String, Bitmap> mbit = new HashMap<String, Bitmap>();
                mbit.put(imgstring, bitmap);

                Intent sendImage = new Intent(DetailsActivity.this, FullImage.class);
                sendImage.putExtra("key", imgstring);
                startActivity(sendImage);
            }
        });




    }


}

FullImage.java(我在上一个活动点击“创建”按钮后没有任何内容显示的第二个活动)

package com.jobaka.www.jobaka;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageView;

public class FullImage extends AppCompatActivity {

    ImageView finalimage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_full_image);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        Intent intent = getIntent();
        String imgstring = intent.getExtras().getString("key");


        //String imgstring = startingIntent.getStringExtra("pic");
        Bitmap bitmap = BitmapFactory.decodeFile(imgstring);
        finalimage = (ImageView) findViewById(R.id.ivFull);
        finalimage.setImageBitmap(bitmap);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

}

1 个答案:

答案 0 :(得分:0)

发送

Bitmap bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),
                            Bitmap.Config.ARGB_8888);  
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream ); 
byte[] b = byteArrayOutputStream .toByteArray();
intent.putExtra("key", b);

接收:

Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("key");    
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
finalimage.setImageBitmap(bitmap );