Click here to Skip to main content
15,889,877 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
this is simple image capture App..

In this code,i use intent for camera and after capture image and press ok from camera it set to image view

but when i try toast the path of image from intent it returns a null pointer exception.

Why does it return null????

if the image is set to the imageview so it store at any place in android emulator so why does it return null??


Java
public class CameraAppActivity extends Activity implements OnClickListener {
    Button btnsetwall;
    ImageButton imgbtntakeph;
    ImageView Imview;
    static final int REQUEST_IMAGE_CAPTURE = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera_app);
        Imview = (ImageView) findViewById(R.id.imageView1);
        imgbtntakeph = (ImageButton) findViewById(R.id.imageButton1);
        btnsetwall = (Button) findViewById(R.id.button1);
        imgbtntakeph.setOnClickListener(this);
        btnsetwall.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.camera_app, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.imageButton1:
            dispatchTakePictureIntent();
        case R.id.button1:

            break;
        }

    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            Imview.setImageBitmap(imageBitmap);
            Toast.makeText(CameraAppActivity.this,"Here "+ data.getData(), Toast.LENGTH_LONG).show();
        }
    }
} 
Posted
Updated 12-Apr-17 18:22pm

1 solution

Try

Java
@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");

            Imview.setImageBitmap(imageBitmap);

            // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
            Uri tempUri = getImageUri(getApplicationContext(), imageBitmap);

            // CALL THIS METHOD TO GET THE ACTUAL PATH
            Toast.makeText(CameraAppActivity.this,"Here "+ getRealPathFromURI(tempUri),                 Toast.LENGTH_LONG).show();
        }
    }

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null); 
    cursor.moveToFirst(); 
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
    return cursor.getString(idx); 
}


For more info check http://stackoverflow.com/questions/20327213/getting-path-of-captured-image-in-android-using-camera-intent[^]

Hope this helps.
 
Share this answer
 
v2
Comments
priyanshbhaliya 9-Mar-14 12:17pm    
thnks a lot.....
BupeChombaDerrick 9-Mar-14 15:49pm    
You are welcome.
priyanshbhaliya 11-Mar-14 4:09am    
can u explain why intent directly give null path of image??
BupeChombaDerrick 11-Mar-14 11:49am    
It seems you have to insert the image using "Images.Media.insertImage()" for it to be saved to SD card and so that the MediaStore can create a path for it. The intent seems only to return the raw image data. But I'am not very sure.
priyanshbhaliya 11-Mar-14 12:37pm    
ok...and when i mycode test in real device it return correct path rather then null..
it make confusion to me because..."in emulator" it return null and in "real device" it return full image path..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900