Delphinium
Delphinium

Reputation: 105

Transfer Image picked from gallery to another activity

I am trying to send m image which is selected by either gallery or camera to another activity but nothing seems to be happening, I have converted the Bitmap to string to send it over the intent but it seems that the intent never gets invoked.

Please help me what am i doing wrong?

this is my onActivityResult() Code

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            Bitmap thumbnail = null;
            if(requestCode==RESULT_LOAD_IMAGE){
               if(resultCode==RESULT_OK){

                    Uri selectedImageUri = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };
                    Cursor cursor = getContentResolver().query(selectedImageUri,filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    data.getExtras().get("data");

                    thumbnail = (BitmapFactory.decodeFile(picturePath));

                    //CONVERT BITMAP TO STRING
                    ByteArrayOutputStream baos=new  ByteArrayOutputStream();
                    thumbnail.compress(Bitmap.CompressFormat.PNG,100, baos);
                    byte [] b=baos.toByteArray();
                    String temp=Base64.encodeToString(b, Base64.DEFAULT);
                        //trying to start activity...

                    Intent intent = new Intent(Home.this, Upload.class);
                    intent.putExtra("picture", temp);
                    startActivity(intent);}

                }
            }

            }

Upvotes: 2

Views: 3646

Answers (2)

Julian Mancera
Julian Mancera

Reputation: 304

Instead of converting the image to string, why don't you work with the path to the file and reload it again in the other activity? I was facing the same issue with my app Pic4Share and I solved it in that way.

In the activity that loads the image from the gallery:

String stringPath;

protected void onActivityResult(int requestCode, int resultCode,Intent imageReturnedIntent) {
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 
        switch(requestCode) { 
            case SELECT_PHOTO:
                if(resultCode == RESULT_OK){  
                    Uri selectedImage = imageReturnedIntent.getData();
                    String[] filePathColumn = {MediaStore.Images.Media.DATA};
                    Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
                    cursor.moveToFirst();

                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    stringPath = cursor.getString(columnIndex); // ***Here is stored the path***
                    cursor.close();

                    BitmapFactory.Options opts = new BitmapFactory.Options ();
                    opts.inSampleSize = 4;   // for 1/2 the image to be loaded
                    opts.inPurgeable = true;

                    image.setImageBitmap(decodeSampledBitmapFromFile(txtPath, 800, 800));

                }
                else{
                    //Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

Then you can pass the string with the path using the intent:

Intent intent = new Intent(Home.this, Upload.class);
Bundle b = new Bundle();
b.putString("picture", stringPath);
intent.putExtras(b);
startActivity(intent);

Finally, in your other application you load the picture again using the path:

Bundle b = getIntent().getExtras();
String picturePath = b.getString("picture");
Bitmap scaledBitmap = decodeSampledBitmapFromFile(picturePath, 150, 150);

Upvotes: 1

Roberto Lombardini
Roberto Lombardini

Reputation: 616

I don't know if u need to transform your bitmap in a String object to respect some logic in your activity. If this is not the case then the conversion bitmap -> String became useless because Bitmap implements Parcelable, so you could pass it via intent with putExtra(String name, Parcelable value) method.

You can find some code at this link

Example:

Send the intent

Intent intent = new Intent(Home.this, Upload.class);
intent.putExtra("picture", thumbnail);

and retrieve it from the other activity

Bitmap bitmap = (Bitmap) intent.getParcelableExtra("picture");

Hope it helps

Upvotes: 0

Related Questions