0

I'm working on this Camera application but I'm having trouble displaying the image that the user takes. The camera, preview, and capture buttons work perfectly on the main activity. Right now, the application saves the photo to Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp" But I DON'T want the images to save there. I just want to pass that image to the next activity, (Not save it to a public directory). Is there a way to do this? I've looked at the tutorials but can't figure out how to implement MODE.PRIVATE.

Here's where it is called:

  @Override
    public void onPictureTaken(final byte[] data, Camera camera) {

        new AsyncTask<Void, Void, String>() {

            @Override
            protected String doInBackground(Void... params) {
                File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
                if (pictureFile == null){
                    Log.d(TAG, "Error creating media file, check storage permissions: ");
                    return null;
                }

                try {
                    FileOutputStream fos = new FileOutputStream(pictureFile);
                    fos.write(data);
                    fos.close();
                    return pictureFile.getPath();
                } catch (FileNotFoundException e) {
                    Log.d(TAG, "File not found: " + e.getMessage());
                    return null;
                } catch (IOException e) {
                    Log.d(TAG, "Error accessing file: " + e.getMessage());
                    return null;
                }
            }

            @Override
            protected void onPostExecute(String result) {
                super.onPostExecute(result);
                if(result != null){
                    Intent intent = new Intent(mContext, ImageDisplayActivity.class);
                    intent.putExtra(ImageDisplayActivity.KEY_PATH, result);
                    startActivity(intent);
                }
            }

        }.execute();


    }

Here is the code for the method:

private static File getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = new     File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
      // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (! mediaStorageDir.exists()){
        if (! mediaStorageDir.mkdirs()){
            Log.d("MyCameraApp", "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",    Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE){
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_"+ timeStamp + ".jpg");
    } else {
        return null;
    }

    return mediaFile;


}
nnnnnnitters
  • 65
  • 10
  • Follow http://stackoverflow.com/questions/12496700/maximum-length-of-intent-putextra-method-force-close – Arun Kumar Feb 26 '15 at 03:49
  • You can't pass large image with intent so best way to store image in temp file and will get it in second activity – Arun Kumar Feb 26 '15 at 03:51

2 Answers2

0

Try this one ,

ActivityOne.Java

try {
    //Write file
    String filename = "photo.png";
    FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
    bmp.compress(Bitmap.CompressFormat.PNG, 200, stream);

    //Cleanup
    stream.close();


    //Pop intent
    Intent mIntent= new Intent(this, ActivityTwo.class);
    in1.putExtra("bitmap", filename);
    startActivity(mIntent);
} catch (Exception e) {
    e.printStackTrace();
}

ActivityTwo.Java

Bitmap bmp = null;
String filename = getIntent().getStringExtra("bitmap");
try {
    FileInputStream is = this.openFileInput(filename);
    bmp = BitmapFactory.decodeStream(is);
    is.close();
} catch (Exception e) {
    e.printStackTrace();
}

and set Imageview with bitmap you can get your bitmap which you captured from camera

Harshal Kalavadiya
  • 2,412
  • 4
  • 38
  • 71
0

http://developer.android.com/guide/topics/data/data-storage.html#filesInternal

Look at the example :

String FILENAME = "hello_file";
String string = "hello world!";

FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();

So basically, you just need to have a FILENAME to create a file on private mode. Just keep the last part your method getOutputMediaFile(int type) it should be something like this :

private static File getOutputMediaFile(int type){
     String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                                              Locale.getDefault()).format(new Date());

     if (type == MEDIA_TYPE_IMAGE) {
          mediaFile =  "IMG_"+ timeStamp + ".jpg";
       } else {
          return null;
       }
      return mediaFile;  
}

and then just change this try of your doInBackground function :

    try {
      FileOutputStream fos = openFileOutput(pictureFile, Context.MODE_PRIVATE);
      fos.write(data);
      fos.close();
      return pictureFile;
     } 

And just give the path to your other Activity and create a bmp

String filepath = intent.getStringExtra("EXTRA_FILE");
java.io.FileInputStream in = openFileInput(filepath);
Bitmap bmp = BitmapFactory.decodeStream(in)
henrily
  • 236
  • 1
  • 5
  • mediaStorageDir is not initialized though. I don't know what directory to make it.... – nnnnnnitters Feb 26 '15 at 04:42
  • Whoops i'll edit the code but, you don't have to set a mediaStorageDir it will be stock on your private Directory (something like mycameraapp.myapplication) You just have to give a name of file – henrily Feb 26 '15 at 04:46