1. Are you ready for the Galaxy S20? Here is everything we know so far!

Taking an Image and Saving it to the Camera Roll

Discussion in 'Android Development' started by someguysomewhere, Nov 11, 2014.

  1. someguysomewhere

    Thread Starter

    First off, I just started experimenting with writing Android apps for about the last week, so I'm still very much a noob at this.

    The problem I'm having is I've written a camera type app that takes an image, using the Android devices built in camera, then sends a display of the image back to the app. It worked fine until I wanted to save the image automatically to the devices camera roll after taking the image. I put in this code and now it wont display the image back into the app like before:

    Code (Text):
    1.  
    2. File file = new File(Environment.getExternalStorageDirectory(), CurrentDateAndTime + ".jpg");
    3. Uri photoPath = Uri.fromFile(file);
    4. intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);
    5.  
    Here is the whole MainActivity.java file:

    Code (Text):
    1.  
    2. package com.example.camera;
    3.  
    4. import java.io.File;
    5. import java.text.SimpleDateFormat;
    6. import java.util.Calendar;
    7. import java.util.List;
    8.  
    9. import android.net.Uri;
    10. import android.os.Bundle;
    11. import android.os.Environment;
    12. import android.provider.MediaStore;
    13. import android.app.Activity;
    14. import android.content.Intent;
    15. import android.content.pm.PackageManager;
    16. import android.content.pm.ResolveInfo;
    17. import android.graphics.Bitmap;
    18. import android.view.View;
    19. import android.view.View.OnClickListener;
    20. import android.widget.Button;
    21. import android.widget.ImageView;
    22. import android.widget.TextView;
    23.  
    24. public class MainActivity extends Activity implements OnClickListener {
    25.  
    26.     Button btnTackPic;
    27.     ImageView ivThumbnailPhoto;
    28.     Bitmap bitMap;
    29.     static int TAKE_PICTURE = 1;
    30.     String CurrentDateAndTime= getCurrentDateAndTime();
    31.  
    32.     @Override
    33.     protected void onCreate(Bundle savedInstanceState)
    34.     {
    35.         super.onCreate(savedInstanceState);
    36.         setContentView(R.layout.activity_main);
    37.  
    38.         btnTackPic = (Button) findViewById(R.id.btnTakePic);
    39.         ivThumbnailPhoto = (ImageView) findViewById(R.id.ivThumbnailPhoto);
    40.  
    41.         btnTackPic.setOnClickListener(this);
    42.     }
    43.  
    44.     private String getCurrentDateAndTime()
    45.     {
    46.         Calendar c = Calendar.getInstance();
    47.         SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
    48.         String formattedDate = df.format(c.getTime());
    49.         return formattedDate;
    50.     }
    51.  
    52.     @Override
    53.     public void onClick(View view)
    54.     {
    55.         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    56.  
    57.         File file = new File(Environment.getExternalStorageDirectory(), CurrentDateAndTime + ".jpg");
    58.         Uri photoPath = Uri.fromFile(file);
    59.         intent.putExtra(MediaStore.EXTRA_OUTPUT, photoPath);
    60.        
    61.         startActivityForResult(intent, TAKE_PICTURE);
    62.     }
    63.  
    64.     @Override
    65.     protected void onActivityResult(int requestCode, int resultCode, Intent intent)
    66.     {
    67.         if (requestCode == TAKE_PICTURE && resultCode== RESULT_OK && intent != null)
    68.         {
    69.             Bundle extras = intent.getExtras();
    70.  
    71.             bitMap = (Bitmap) extras.get("data");
    72.            
    73.             ivThumbnailPhoto.setImageBitmap(bitMap);
    74.         }
    75.     }
    76. }
    77.  
    Does it have to do with EXTRA_OUTPUT returning a null value?
     


  2. codesplice

    codesplice Elite Recognized Moderator
    Moderator

Loading...

Share This Page

Loading...