0

This is my code. I want to add one more button here which onclick send the image to next activity , but I am not able to configure this. How should I do it?

public class MainActivity extends Activity {
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;

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

public void loadImagefromGallery(View view) {
    // Create intent to Open Image applications like Gallery, Google Photos
    Intent galleryIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Start the Intent
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
}



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        // When an Image is picked
        if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK
                && null != data) {
            // Get the Image from data

            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            // Get the cursor
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            // Move to first row
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            imgDecodableString = cursor.getString(columnIndex);
            cursor.close();
            ImageView imgView = (ImageView) findViewById(R.id.imgView);
            // Set the Image in ImageView after decoding the String
            imgView.setImageBitmap(BitmapFactory
                    .decodeFile(imgDecodableString));

        } else {
            Toast.makeText(this, "You haven't picked Image",
                    Toast.LENGTH_LONG).show();
        }
    } catch (Exception e) {
        Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG)
                .show();
    }

}
3
  • 3
    Pass the file name to the next Activity. Commented Jan 23, 2017 at 7:52
  • 1
    You already has string that contain image, just send it using intent extra Commented Jan 23, 2017 at 8:02
  • I write the code for sending using intent extre but how to get it in next activity public void nextActivity(View view){ Intent intent= new Intent(this, DisplayActivity.class); intent.putExtra("bmp_image",imgDecodableString); startActivity(intent); } Commented Jan 23, 2017 at 8:39

4 Answers 4

0

Initially, keep newly added button disable and in onActivityResult method after getting image string enable button and set onClickListener to button. And pass that image string to next activity using intent extra.

In onActivityResult method,

newButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(getApplicationContext(),NextActivity.class).putExtra("img",imgDecodableString));
            }
        });
Sign up to request clarification or add additional context in comments.

6 Comments

i implemented ur code......and jump to next activity and getting blank page there.... and in logcat i get this...... ** Key img expected Parcelable but value was a java.lang.String. The default value <null> was returned. W/Bundle: Attempt to cast generated internal exception:**
how are you getting string in next activity what code you have written in next activity?
this is the code of next activit---------------------------------> _ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Bitmap imgDecodeableString =(Bitmap)this.getIntent().getParcelableExtra("img"); setContentView(R.layout.activity_display); ImageView imgView = (ImageView) findViewById(R.id.imageView); imgView.setImageBitmap(imgDecodeableString); } }
Replace your code in onCreate method String imgDecodeableString =(String)this.getIntent().getStringExtra("img"); setContentView(R.layout.activity_display); ImageView imgView = (ImageView) findViewById(R.id.imageView); imgView.setImageBitmap(BitmapFactory .decodeFile(imgDecodeableString));
thanku bro!! you save me!! I am suffer this from yesterday and now i got my solution!! Thank u so much to help me... Love from India bro
|
0

Best way to do this with large image would be using Serializable, since Intent and Parcelable both have size limit.

Answers to this question might help : Serializing and De-Serializing android.graphics.Bitmap in Java

Comments

0

you can put the data in your first activity like

Intent intent = new Intent(this, NewActivity.class);
intent.putExtra("BitmapImage", bitmap);

and in second activity parse the data like below:

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

convert your imagepath to bitmap code snipped is:

BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(imagePath, options);

4 Comments

if you do not understand properly solution. don't give the devote. intent can only have primitive data type from one activity to another if you want to put data for the image you have to convert it bitmap and parse it above explained if you not interested don't devote it. Thank You !!
it's okay but you have to work around it. if you have a small size of the image it will work .
i am choosing image from gallery and this method is not working error showing Key bmp_image expected Parcelable but value was a java.lang.String. The default value <null> was returned.
You have to convert your selected image file into bitmap and put in the intent. convert the image in bitmap.
0

//First Activity

Bitmap b = null;
String bitmapString = getStringFromBitmap(b);
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("bitmapString", bitmapString);

//Second Activity

String receivedBitmapString = getIntent().getStringExtra("bitmapString");
        Bitmap receivedBitmap = getBitmapFromString(receivedBitmapString);

//Functions

 private String getStringFromBitmap(Bitmap bitmapPicture) {
     /*
     * This functions converts Bitmap picture to a string which can be
     * JSONified.
     * */
     final int COMPRESSION_QUALITY = 100;
     String encodedImage;
     ByteArrayOutputStream byteArrayBitmapStream = new ByteArrayOutputStream();
     bitmapPicture.compress(Bitmap.CompressFormat.PNG, COMPRESSION_QUALITY,
     byteArrayBitmapStream);
     byte[] b = byteArrayBitmapStream.toByteArray();
     encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
     return encodedImage;
     }


    private Bitmap getBitmapFromString(String jsonString) {
    /*
    * This Function converts the String back to Bitmap
    * */
    byte[] decodedString = Base64.decode(stringPicture, Base64.DEFAULT);
    Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
    return decodedByte;
    }

Source:Link

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.