I am a novice in JAVA and I am trying to learn android developement using the Google training documentation. I have written some codes which uses this training module
This is basically invoking a camera intent --> clicking an image and then writing the image to the external root directory of the external storage (SD card). Using the code i can successfully click a photo using the existing camera app and i am able to view the thumbnail, but when I am trying to navigate to the folder (having my package name), i am not able to view the image that was clicked via the app i.e. the folder with my package name exists but there are no image files in it.
As it can be seen from the code below that i have implemented the createImageFile() function to create a image file in the external storage and inside addListenerOnButton() function i have assigned the return value of createImageFile() to the photo uri. Won't this create an image file already? or am i missing any steps in between. I researched a bit and have found that the "write()" method shold be implemented in order to write the image data onto the file path created by the createImageFile() fucntion, but since i am new to this i tried but i cannot figue out where should i implement the write() method and what data should be passed to this.
Below is my code -
package com.app.simple.simplecamera;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.content.FileProvider;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.io.FileOutputStream;
import java.io.IOException;
import android.widget.Toast;
import static android.app.Activity.RESULT_OK;
/**
* A placeholder fragment containing a simple view.
*/
public class MainActivityFragment extends Fragment {
public MainActivityFragment() {
}
/**creating View instance*/
View rootview;
/**setting image capture intent to 1**/
static final int REQUEST_IMAGE_CAPTURE = 1;
//static final int RESULT_OK = 1;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/**instantiating rootview to infalter,so that whenever
* fragment is inflated the camera fucntion will be called**/
rootview = inflater.inflate(R.layout.fragment_main, container, false);
/**calling the public funtion addlistenerbutton so as to invoke camera intent**/
addListenerOnButton();
// getActivity().setResult(RESULT_OK);
/**return the rootview instance whenever oncreate function is invoked in fragment**/
return rootview;
}
/**setting image capture intent to 1**/
//static final int REQUEST_IMAGE_CAPTURE = 1;
//static final int RESULT_OK = 1;
//Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
/**public function for registering button action**/
/**public void addListenerOnButton(){*/
String mCurrentPhotoPath;
public File createImageFile() throws IOException{
//create a timestamp for the file
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
//the filename should be as below
String imageFileName = "JPEG" + timestamp + "_";
/**assiging a directory to store the file
here the getExternalFilesDir is accessed via the getActivity()(since it's inside a fragement
and DIRECTORY_PICTURES on the device is allocated for storage**/
File storageDir = getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
//a temp file is created
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
//FileOutputStream out = new FileOutputStream(image);
//out.write(mCurrentPhotoPath.getBytes());
//galleryAddPic();
return image;
}
public void addListenerOnButton(){
/**creating a button instance and assiging it to the
* oncreateview function via the rootview object*/
Button button = (Button) rootview.findViewById(R.id.button);
/**setOnClickListener contains the camera action within it**/
button.setOnClickListener(new View.OnClickListener(){
/**onClick fucntion to invoke the exisitng camera app via the camera intent**/
public void onClick(View v) {
/*Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);*/
/**if camera exists, which is checked by the getpackagemanager fucntion, then capture image**/
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
//startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
Context context = getContext().getApplicationContext();
// Create the File where the photo should go
File photoFile = null;
try{
photoFile = createImageFile();
}
catch (IOException ex){
// Error occurred while creating the File
Toast toast = Toast.makeText(context, "There was a problem saving the photo...", Toast.LENGTH_SHORT);
toast.show();
}
// File pictureFile = createPhotoFile();
// Continue only if the File was successfully created
if (photoFile != null){
//Uri photoURI = FileProvider.getUriForFile(this, "com.app.simple.simplecamera.fileprovider", photoFile);
Uri photoURI = FileProvider.getUriForFile(context, "com.app.simple.simplecamera.fileprovider", photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
//getActivity().setResult(RESULT_OK,takePictureIntent);
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
//startActivityForResult(takePictureIntent, RESULT_OK)
}
}
}
});
}
@Override
///**overriding the onactivityresult method so that it creates a tumbnail
// * it takes int he request_image_capture value from addlistenronbutton method
// * result code formthe camera activity inside the addlistneronbiutton method
// * and data from the intent that invokes the camera and takes picture**/
public void onActivityResult(int requestCode, int resultCode, Intent data){
/**checking if request i simagecapture and result is ok**/
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
Bundle extras = data.getExtras();
/**creating a data bundle with teh object extra**/
Bundle extras = data.getExtras();
/**creating bitmap from the captured image data which is etched from
* the intent data inside addlistneronbutton**/
Bitmap imageBitmap = (Bitmap) extras.get("data");
/**creating a placeholder for the image thumbnail using the Imageview instance and the corresponding xml image placeholder "result"
* refer the fragment xml for the image placeholder**/
ImageView imageView = (ImageView)rootview.findViewById(R.id.result);
imageView.setImageBitmap(imageBitmap);
}
}
}
Edit- I forgot to mention that the onActivityResult() throws a null pointer exception on 'boolean android.graphics.Bitmap.compress
The following is my stacktrace- AndroidRuntime: FATAL EXCEPTION: main Process: com.app.simple.simplecamera, PID: 641 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=null} to activity {com.app.simple.simplecamera/com.app.simple.simplecamera.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference at android.app.ActivityThread.deliverResults(ActivityThread.java:3948) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3991) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1542) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5769) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)' on a null object reference at com.app.simple.simplecamera.MainActivityFragment.onActivityResult(MainActivityFragment.java:176) at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:175) at android.app.Activity.dispatchActivityResult(Activity.java:6845) at android.app.ActivityThread.deliverResults(ActivityThread.java:3944) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3991) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1542) at android.os.Handler.dispatchMessage(Handler.java:111) at android.os.Looper.loop(Looper.java:207) at android.app.ActivityThread.main(ActivityThread.java:5769) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) I/Process: Sending signal. PID: 641 SIG: 9 Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'