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?
Try replacing your line:
with this one:
I really advise you to use a library to do the hard work to you. You must take a look on this library, it's much more easy to pick an imagem from gallery or from camera. With the above library an dialog will show up and the user will be able to choose if want get the image from gallery or from camera.
Just call to show the dialog:
Make your activity implements this:
And then override this method:
Try this way,hope this will help you to solve your problem.
Add this permission in AndroidManifest.xml:
Please have a look at the following it might help you:
Change camera code as below.
}