0

I Updated my code with @CommonWare's suggestions.

private static final String EXTRA_FILENAME = "qamatris.novi.com.tr.EXTRA_FILENAME";
private static final String FILENAME =  UUID.randomUUID().toString().replaceAll("-", "");;

 @Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putSerializable(EXTRA_FILENAME, output);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 4) {

Uri uri =Uri.fromFile(output);
Bitmap photo = getScaledBitmapFromUri(ctx, uri);
ByteArrayOutputStream stream2 = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream2);
byte[] resarray = stream2.toByteArray();    }   

***How to call camera intent***

Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (savedInstanceState==null) {
     File dir=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
     dir.mkdirs();
     output=new File(dir, FILENAME);
                }
                else {
                    output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
                }

                if (output.exists()) {
                    output.delete();
                }

                i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

                startActivityForResult(i, CONTENT_REQUEST);

Now im getting NullpointerException:file " Uri uri =Uri.fromFile(output);" because of output is null. this line ( output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);)

  • Because `takePictureIntent` create new phono and not sent bitmap on intent. Try http://stackoverflow.com/q/10042695/4149649 – Yuri Misyac Apr 14 '16 at 11:18
  • @YuriMisyac takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile)); –  Apr 14 '16 at 11:34

2 Answers2

1

Try this code for onActivityResult(), I hope it will work. You can get full size photo captured by camera through targetUri

First you have to get cameraImageUri when image file created by your code. Use it:

cameraImageUri = Uri.fromFile(createImageFile());

Then you have to get intent callback onActivityResult() and do this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == CAMERA_PIC_REQUEST || requestCode == GALLERY_PIC_REQUEST){
        if(resultCode == Activity.RESULT_OK) {
            if (requestCode == CAMERA_PIC_REQUEST) {
                targetUri = cameraImageUri;
            }
            else{
                targetUri = data.getData();
            }
            Log.i(TAG, "Image path: " + targetUri);

            if(targetUri != null) {
                DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
                Bitmap photo = getScaledBitmapFromUri(getContext(), targetUri);
                imageView.setImageBitmap(photo);
            } else {
                CommonUtilities.toastShort(getContext(), "Unable to retrieve image, please retake...");
            }
        }
    }
}

public static Bitmap getScaledBitmapFromUri(Context context, Uri uriImageFile) {
    Bitmap scaledBitmap = null;
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inSampleSize = 4;
    try {
        BitmapFactory.decodeStream(context.getContentResolver()
                .openInputStream(uriImageFile), null, options);
    } catch (FileNotFoundException e1) {
        e1.printStackTrace();
        return null;
    }

    int srcWidth = options.outWidth;
    int scale = 1;
    while (srcWidth / 2 > 60) {
        srcWidth /= 2;
        scale *= 2;
    }

    options.inJustDecodeBounds = false;
    options.inDither = false;
    options.inSampleSize = scale;

    try {
        scaledBitmap = BitmapFactory.decodeStream(context
                .getContentResolver().openInputStream(uriImageFile), null, options);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }

    return scaledBitmap;
}
Hassan Jamil
  • 951
  • 1
  • 13
  • 33
  • it gives thumbnail ithink. Because quality is very low. also why u create metrics variable if u dont use? –  Apr 14 '16 at 13:32
  • No You can take a scaled bitmap for displaying purpose from this code when required, also to avoid Out of memory exception, otherwise from targetUri (physical path) you can get the real/unscaled/uncropped version of image file. – Hassan Jamil Apr 14 '16 at 13:59
  • What i need to increase in getScaledBitmapFromUri function for alittle bit quality? –  Apr 14 '16 at 19:15
  • I have found a very nice and working tutorial for bitmap scaling, please follow the link: http://developer.sonymobile.com/2011/06/27/how-to-scale-images-for-your-android-application/ – Hassan Jamil Apr 15 '16 at 05:40
  • Also you can download its source project from here: http://developer.sonymobile.com/downloads/code-example-module/image-scaling-code-example-for-android/ – Hassan Jamil Apr 15 '16 at 05:41
0

So what im doing wrong ?

You set EXTRA_OUTPUT, and then ignored it. Your photo may be in the location that you provided to EXTRA_OUTPUT. I say "may" because you are using ACTION_IMAGE_CAPTURE to ask a random third-party app to take the picture, and such apps sometimes have bugs.

This activity, from this sample app, takes a picture using ACTION_IMAGE_CAPTURE, then asks a third-party app to view the resulting picture using ACTION_VIEW:

/***
 Copyright (c) 2008-2016 CommonsWare, LLC
 Licensed under the Apache License, Version 2.0 (the "License"); you may not
 use this file except in compliance with the License. You may obtain a copy
 of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
 by applicable law or agreed to in writing, software distributed under the
 License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 OF ANY KIND, either express or implied. See the License for the specific
 language governing permissions and limitations under the License.

 From _The Busy Coder's Guide to Android Development_
 https://commonsware.com/Android
 */

package com.commonsware.android.camcon;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;

public class CameraContentDemoActivity extends Activity {
  private static final String EXTRA_FILENAME=
    "com.commonsware.android.camcon.EXTRA_FILENAME";
  private static final String FILENAME="CameraContentDemo.jpeg";
  private static final int CONTENT_REQUEST=1337;
  private File output=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (savedInstanceState==null) {
      File dir=
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

      dir.mkdirs();
      output=new File(dir, FILENAME);
    }
    else {
      output=(File)savedInstanceState.getSerializable(EXTRA_FILENAME);
    }

    if (output.exists()) {
      output.delete();
    }

    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

    startActivityForResult(i, CONTENT_REQUEST);
  }

  @Override
  protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    outState.putSerializable(EXTRA_FILENAME, output);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
    if (requestCode == CONTENT_REQUEST) {
      if (resultCode == RESULT_OK) {
        Intent i=new Intent(Intent.ACTION_VIEW);

        i.setDataAndType(Uri.fromFile(output), "image/jpeg");
        startActivity(i);
        finish();
      }
    }
  }
}

Note that I am using the same value for the Uri to ACTION_VIEW as I used in the EXTRA_OUTPUT extra.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • so with my code is it normal to Indent data is null ? –  Apr 14 '16 at 11:38
  • I ımplemented ur suggestions to my code but still my intent is null. no luck –  Apr 14 '16 at 12:14
  • @keikoman: **The `Intent` is *supposed* to be `null`**. Do not look at the `Intent`. The `Intent` is useless. You *know* where the image is supposed to be, because **you told the app where to put the image** via `EXTRA_OUTPUT`. So, look there for your image. – CommonsWare Apr 14 '16 at 12:15