-1

I'm new to android development. I am trying to create an app where when a user take a picture, it's path will be saved to the database. I am able to save and retrieve the image when it's from the gallery, But how do I save it if it's taken by camera?

Here's My Code

private void selectImage() {
                final CharSequence[] items = { "Take Photo", "Choose from Library",
                        "Cancel" };

                AlertDialog.Builder builder = new AlertDialog.Builder(Student_Profile.this);
                builder.setTitle("Add Photo");
                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int item) {
                        if (items[item].equals("Take Photo")) {
                            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                            File f = new File(android.os.Environment
                                   .getExternalStorageDirectory(), "temp.jpg");

                           intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                           startActivityForResult(intent, 1);

                        } else if (items[item].equals("Choose from Library")) {
                            Intent intent = new Intent(
                                    Intent.ACTION_PICK,
                                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                            intent.setType("image/*");
                            startActivityForResult(
                                    Intent.createChooser(intent, "Select File"),
                                    0);
                        } else if (items[item].equals("Cancel")) {
                            dialog.dismiss();
                        }
                    }
                });
                builder.show();
            }

        @Override
            public void onActivityResult( int requestCode, int resultCode, Intent data) {
                 super.onActivityResult(requestCode, resultCode, data);


                 if (resultCode == Activity.RESULT_OK){
                    if (requestCode == 0) {
                        studentsDbAdapter.delete_Pic_byID(studID);
                     Uri targetUri = data.getData();
                     picture_location=getPath(targetUri);
                    // picture_location = targetUri.toString();             
                     studentsDbAdapter.insertImagePath(studID ,picture_location);
                     showpic();
                 }   }

                 else if (requestCode == 1) {
                    // How do I get The URI?
                    // I'm thinking that maybe if I can get the Uri 
                    //from the new image I can save it like how I saved the image to my DB gallery style.
                    String pic_location=getPath(Uri);
                     studentsDbAdapter.insertImagePath(studID ,pic_location);
                     showpic();
                 }
                 }

             public String getPath(Uri uri) {
                 String[] projection = { MediaStore.Images.Media.DATA };
                 @SuppressWarnings("deprecation")
                Cursor cursor = managedQuery(uri, projection, null, null, null);
                 int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                 cursor.moveToFirst();
                 return cursor.getString(column_index);
             }

            public void showpic() {

                //LoginDataBaseAdapter db = studentsDbAdapter.open();
                 boolean emptytab = false;
                 boolean empty = studentsDbAdapter.checkPic(null, emptytab);

                //Cursor cursor = loginDataBaseAdapter.fetchProfileImageFromDatabase();  

                 if(empty==false)  
                  {  



                    String pathName = studentsDbAdapter.getImapath(studID);
                    File image = new  File(pathName); 
                    if(image.exists()){
                             ImageView imageView= (ImageView) findViewById(R.id.studpic);
                             imageView.setImageBitmap(BitmapFactory.decodeFile(image.getAbsolutePath()));
                }
                  }
                 } 
DEADPOOL
  • 83
  • 2
  • 15
  • look into this [post](http://stackoverflow.com/q/12995185/3326331) – Sagar Pilkhwal Sep 17 '14 at 09:13
  • Uri u = intent.getData(); – Deniz Sep 17 '14 at 09:14
  • @deniz I already tried but it gives me the error 09-17 17:44:11.238: E/AndroidRuntime(26174): FATAL EXCEPTION: main 09-17 17:44:11.238: E/AndroidRuntime(26174): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {com.csu.eclassrecord/com.csu.eclassrecord.Student_Profile}: java.lang.NullPointerException 09-17 17:44:11.238: E/AndroidRuntime(26174): at android.app.ActivityThread.deliverResults(ActivityThread.java:3513) – DEADPOOL Sep 17 '14 at 09:45
  • Check the below answer it may help you – Deniz Sep 17 '14 at 09:54

1 Answers1

0

Try this. It may help you.

if (fromGallery) {

   data.getData()); //get image data like this

} else {

bm = (Bitmap) data.getParcelableExtra("data");

}
Deniz
  • 12,332
  • 10
  • 44
  • 62