0

i am trying to show a pic clicked from camera on another activity image view,but getting image out of the bundle into bitmaps.. i get this error. my code is

package com.example.iwm;

import java.io.File;

import java.text.SimpleDateFormat;


import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.widget.Toast;

public class MainActivity extends Activity {

private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
//private static final int RESULT_OK = -1;
private static final int MEDIA_TYPE_IMAGE = 1;

public static Bundle s = null;
//public static String y ;
private Uri fileUri;
private static Intent intent;
private static Intent intent2;

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

      // create Intent to take a picture and return control to the calling application
     intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
    intent.putExtra(android.provider.MediaStore.ACTION_IMAGE_CAPTURE, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
    //onActivityResult(CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE, int resultCode, intent)
}


private Uri getOutputMediaFileUri(int type) {
    // TODO Auto-generated method stub
      return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
@SuppressLint("SimpleDateFormat")
private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "IWMP-Images");
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("IWMP-Images", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new java.util.Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
        "IMG_"+ timeStamp + ".jpg");
    }  else {
        return null;
    }

    return mediaFile;
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
           Toast.makeText(this, "Image saved to:\n" + fileUri, Toast.LENGTH_LONG).show();
            s =data.getExtras();

             intent2 = new Intent(MainActivity.this,MainActivity2.class);

             intent2.putExtra("Image", s);

            intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            getApplicationContext().startActivity(intent2);



        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
             Toast.makeText(this, "Image NOT saved ", Toast.LENGTH_LONG).show();
        }
    }
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

  }

`

from this i an saving the data(Intent) in a bundle S and sending it to activty2

my activity2 code is

  package com.example.iwm;

  import android.os.Bundle;
  import android.app.Activity;
  import android.graphics.Bitmap;

  import android.view.Menu;
  import android.view.View;
  import android.widget.ImageView;
  import android.widget.TextView;

 public class MainActivity2 extends Activity {
 TextView tx;
 Bitmap bmp;
 ImageView ivUserImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_activity2);
    ivUserImage = (ImageView)findViewById(R.id.image);
    tx = (TextView)findViewById(R.id.text1);
    //tx.setText(MainActivity.y);
 Bundle bundle = getIntent().getExtras();
 try
 {
 bmp =(Bitmap)bundle.get("Image");

 }
 catch(Exception e)
 {
     tx.setText(e.toString());
}
 ivUserImage.setImageBitmap(bmp);
 //int  i=10;
}


public void upload(View view)
{

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main_activity2, menu);
    return true;
}

  }
Yogesh Kumar
  • 682
  • 1
  • 10
  • 29
  • Your are sending bundle though intent instead of Bitmap: `s =data.getExtras();` `intent2.putExtra("Image", s);` Use: `intent2.putExtra("Image", s.get("data"))` – andrew May 20 '13 at 07:20
  • andrew_pako .. when using .. intent2.putExtra("Image", s.get("data")).. The method putExtra(String, boolean) in the type Intent is not applicable for the arguments (String, Object)..means s.get("data")..gives boolean value. – Yogesh Kumar May 20 '13 at 07:54
  • I forgot that method is overloaded. In such case you need to do cast to `Bitmap`: Use: intent2.putExtra("Image", (Bitmap)s.get("data")) – andrew May 20 '13 at 09:33

2 Answers2

1

can u try like this

get the image path after u taken from camera like this

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
    if (resultCode == RESULT_OK) {

final String[] imageColumns = { MediaStore.Images.Media._ID,
            MediaStore.Images.Media.DATA };
    final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
    Cursor imageCursor = managedQuery(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns,
            null, null, imageOrderBy);
    if (imageCursor.moveToFirst()) {

        String pathoflasttakenimage = imageCursor.getString(imageCursor
                .getColumnIndex(MediaStore.Images.Media.DATA));
    }
}

pass this imagepath in bundle like this

Intent intent = new Intent(MainActivity.this, MainActivity2.class);



    Bundle bundle = new Bundle();
    bundle.putString("path", pathoflasttakenimage);

    intent.putExtras(bundle);

    startActivity(intent);

in another activity use this imagepath to show image in imageview like this

Bundle b = getIntent().getExtras();
   String path = b.getString("path");

    BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 2; // for 1/2 the image to be loaded
        Bitmap thumb = Bitmap.createScaledBitmap(
                BitmapFactory.decodeFile(path, opts), 96, 96, false);
        ivUserImage.setImageBitmap(thumb);

let tell me what's problem in this way u face.

Senthil
  • 1,244
  • 10
  • 25
  • my bundle is storing the intent properly.. one thing i'll do i m posting the whole code.. plz check where am i doin it wrong. thank you, – Yogesh Kumar May 20 '13 at 07:07
  • ive also tried Bitmap bitmap = BitmapFactory.decodeFile(MainActivity.fileUri.toString()); ivUserImage.setImageBitmap(bitmap); but this too givin bitmap = null. where my fileUri is having the correct path – Yogesh Kumar May 20 '13 at 08:27
  • have u declared fileUri as static – Senthil May 20 '13 at 08:35
  • it worked thanx.. but i have a question.. u r using cursor, cnt i usr SIMPLE uri.. I MEAN I M HAVING DIFFUCULTY UNDERSTNDIN UR CODE. :p N I WANNA SAVE DESE IMAGES TO A PARTICULAR FOLDER. – Yogesh Kumar May 20 '13 at 09:02
0

use bundle.get("data") instead bundle.get("Image") for getting Bitmap from Bundle in Second Activity. try it as:

Bundle bundle = getIntent().getExtras();
 try
 {
      // getting error here.
   bmp = (Bitmap)bundle.get("data");   //<< use data instead of Image
   ivUserImage.setImageBitmap(bmp);
 }
 catch(Exception e)
 {
     tx.setText(e.toString());}
 }
ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • till (Bitmap)bundle.get("data");..i m getting this Bundle[{Image=Bundle[mParcelledData.dataSize=76944]}] on debugger, bt bmp is still null..?? no image on image view.!!! ive posted my whole code plz check if possible .. thank you – Yogesh Kumar May 20 '13 at 07:15
  • i thnk may be sumwhere dwn the line my image is not getting saved in the bundle, thou its getting saved on sd card.:P – Yogesh Kumar May 20 '13 at 07:17
  • ive also tried Bitmap bitmap = BitmapFactory.decodeFile(MainActivity.fileUri.toString()); ivUserImage.setImageBitmap(bitmap); but this too givin bitmap = null. where my fileUri is having the correct path – Yogesh Kumar May 20 '13 at 08:31