简体   繁体   中英

Pass image through intent using URI to another activity

I am a beginner in programming. I am trying to create Meme generator app. I have got to a point where I display an array of images in a GridView, user can click on one image and it will take them to a new activity where user can enter top and bottom text.

I would appreciate your help with below based on the code I have.

  • A) I am trying to convert a Relative Layout (two TextViews and one ImageView) in a bitmap. Is the code correct?
  • - B) I want to send bitmap created from Relative Layout to FullImage.java activity when user clicks on Create button. Is the code correct you think?
  • - C) When Create button is clicked by the user, currently I get "Failed Binder Transaction". I think its because I am passing an image. I tried to understand and use how to pass URI but was not
    successful. I tried to find snippet on SO and other blogs but was not successful. Is it possible for you to give me detail code? (As a beginner, Ill be thankful!)

DetailsActivity.java

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;

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

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

    @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);

                Intent sendImage = new Intent(DetailsActivity.this, FullImage.class);
                sendImage.putExtra("pic", bitmap);
                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 startingIntent = getIntent();
        String whatYouSent = startingIntent.getStringExtra("pic");
        Bitmap bitmap = BitmapFactory.decodeFile("pic");
        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();
            }
        });
    }

}

In your FullImageActivity, do this to retrieve the bitmap

   Intent intent = getIntent();
   Bitmap bitmap = (Bitmap) intent.getParcelableExtra("pic"); 

This is because the bitmap class in android implements parcelable. http://developer.android.com/reference/android/graphics/Bitmap.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM