user3852672 user3852672 - 1 month ago 11
Java Question

trying to get an image from gallery or camera in android

I am trying out an online tutorial for loading image in an imageview from either the gallery or camera.The gallery part works fine, but the camera part force closes.The data retrieved from onactvitiyresult shows null.

The code:

package com.example.cameragallerypro;

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {

Uri selectedImageUri;
String selectedPath;
ImageView preview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button) findViewById(R.id.bGallery);
Button bCam= (Button) findViewById(R.id.bCamera);
preview = (ImageView) findViewById(R.id.preview);
bCam.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 100);
}
});


b.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
openGallery(10);
}
});
}



public void openGallery(int req_code){

Intent intent = new Intent();

intent.setType("image/*");

intent.setAction(Intent.ACTION_GET_CONTENT);

startActivityForResult(Intent.createChooser(intent,"Select file to upload "), req_code);

}

public void onActivityResult(int requestCode, int resultCode, Intent data) {



if (resultCode == RESULT_OK) {
if(data.getData() != null){
selectedImageUri = data.getData();
}else{
Log.d("selectedPath1 : ","Came here its null !");
Toast.makeText(getApplicationContext(), "failed to get Image!", 500).show();
}

if (requestCode == 100 && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
selectedPath = getPath(selectedImageUri);
preview.setImageURI(selectedImageUri);
Log.d("selectedPath1 : " ,selectedPath);

}

if (requestCode == 10)

{

selectedPath = getPath(selectedImageUri);
preview.setImageURI(selectedImageUri);
Log.d("selectedPath1 : " ,selectedPath);

}

}

}


public String getPath(Uri uri) {

String[] projection = { MediaStore.Images.Media.DATA };

Cursor cursor = managedQuery(uri, projection, null, null, null);

int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

cursor.moveToFirst();

return cursor.getString(column_index);

}

}


The logcat:

09-17 10:09:30.132: E/AndroidRuntime(7300): FATAL EXCEPTION: main
09-17 10:09:30.132: E/AndroidRuntime(7300): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=100, result=-1, data=Intent { act=inline-data (has extras) }} to activity {com.example.cameragallerypro/com.example.cameragallerypro.MainActivity}: java.lang.NullPointerException
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.app.ActivityThread.deliverResults(ActivityThread.java:2536)
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2578)
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.app.ActivityThread.access$2000(ActivityThread.java:117)
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:965)
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.os.Looper.loop(Looper.java:130)
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.app.ActivityThread.main(ActivityThread.java:3689)
09-17 10:09:30.132: E/AndroidRuntime(7300): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 10:09:30.132: E/AndroidRuntime(7300): at java.lang.reflect.Method.invoke(Method.java:507)
09-17 10:09:30.132: E/AndroidRuntime(7300): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
09-17 10:09:30.132: E/AndroidRuntime(7300): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
09-17 10:09:30.132: E/AndroidRuntime(7300): at dalvik.system.NativeStart.main(Native Method)
09-17 10:09:30.132: E/AndroidRuntime(7300): Caused by: java.lang.NullPointerException
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.content.ContentResolver.acquireProvider(ContentResolver.java:743)
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.content.ContentResolver.query(ContentResolver.java:256)
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.app.Activity.managedQuery(Activity.java:1550)
09-17 10:09:30.132: E/AndroidRuntime(7300): at com.example.cameragallerypro.MainActivity.getPath(MainActivity.java:104)
09-17 10:09:30.132: E/AndroidRuntime(7300): at com.example.cameragallerypro.MainActivity.onActivityResult(MainActivity.java:79)
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.app.Activity.dispatchActivityResult(Activity.java:3908)
09-17 10:09:30.132: E/AndroidRuntime(7300): at android.app.ActivityThread.deliverResults(ActivityThread.java:2532)
09-17 10:09:30.132: E/AndroidRuntime(7300): ... 11 more


How do i modify the code so that the camera part works fine?

Answer
bCam.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                from_camera();

            }
        });

        b.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                from_gallery();

            }
        });


public void from_camera() {

        Intent cameraIntent = new Intent(
                android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(cameraIntent, 111);

    }


public void from_gallery() {
        Intent intent = new Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(intent, 222);


    }





    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub

        if (resultCode == RESULT_OK && requestCode == 111) {
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            preview.setImageBitmap(photo);



        } else if (resultCode == RESULT_OK && requestCode == 222) {

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

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();


            preview.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        super.onActivityResult(requestCode, resultCode, data);
    }