0

I'm now making a simple android app. It's just allow user to take an Photo and then show it. When i test it in Virtual device, it's ok. But when i download apk to my android device, after i take a photo in Back camera, the app has stopped and return to main menu. Just problem in Back Camera. In addition, in virtual device, after taking photo, the photo will show successfully. But it's empty in my phone

In MainActivity, i click on "Take Photo", it will start the camera and same my image to folder. Then it send the path of Photo to the next activity and show it.

This is my MainActivity

public class MainActivity extends AppCompatActivity {
    private static final int REQUEST_ID_IMAGE_CAPTURE = 100;
    Button TakePhoto, InsertPhoto, Exit;
    String mCurrentPhotoPath;

    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(imageFileName,".jpg",storageDir);


        mCurrentPhotoPath = image.getAbsolutePath();
        return image;
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TakePhoto = (Button) findViewById(R.id.button);
        InsertPhoto = (Button) findViewById(R.id.button3);
        Exit = (Button) findViewById(R.id.button2);
        //Start Camera
        TakePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                if (intent.resolveActivity(getPackageManager()) != null) {
                    // Create the File where the photo should go
                    File photoFile = null;
                    try {
                        photoFile = createImageFile();
                    } catch (IOException ex) {

                        System.out.println(ex);
                    }
                    // Continue only if the File was successfully created
                    if (photoFile != null) {
                        Uri photoURI = Uri.fromFile(photoFile);
                        intent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                        startActivityForResult(intent,REQUEST_ID_IMAGE_CAPTURE);
                    }

                }

            }
        });


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_ID_IMAGE_CAPTURE) {
            if (resultCode == RESULT_OK) {
                //Bitmap bp = (Bitmap)data.getExtras().get("data");
                //ByteArrayOutputStream stream = new ByteArrayOutputStream();
               // bp.compress(Bitmap.CompressFormat.PNG, 100, stream);
               // byte[] images = stream.toByteArray();
                File imgFile = new  File(mCurrentPhotoPath);
                if(imgFile.exists()){
                    System.out.println("This is file"+mCurrentPhotoPath.toString());
                    Intent Show = new Intent(MainActivity.this, ShowPhoto.class);

                    Show.putExtra("image",imgFile);

                    startActivity(Show);
                }



            } else if (resultCode == RESULT_CANCELED) {
                Toast.makeText(this, "Action canceled", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Action Failed", Toast.LENGTH_LONG).show();
            }
        }

    }
}

This is The ShowPhoto Activity

public class ShowPhoto extends Activity {
private LinearLayout Image;
Button Back,Next;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_show_photo);
    Image=(LinearLayout)findViewById(R.id.linearLayout);
    //Get Image from previous Activity
    File image = (File)getIntent().getExtras().get("image");
    Bitmap bmp = BitmapFactory.decodeFile(image.getAbsolutePath());

    ImageView imageView = new ImageView(getApplicationContext());

    imageView.setImageBitmap(Bitmap.createScaledBitmap(bmp, bmp.getWidth()*2, bmp.getHeight()*2, true));

    Image.addView(imageView);





}

}

This is what's in my logcat when i run app enter image description here

enter image description here

1 Answers1

0

Use should ask for the permission at runtime and declare the permission in Manifest

  if (ContextCompat.checkSelfPermission(getContext(),
            Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

        if (ActivityCompat.shouldShowRequestPermissionRationale((Activity)
                getContext(), Manifest.permission.CAMERA)) {


        } else {
            ActivityCompat.requestPermissions((Activity) getContext(), 
                    new String[]{Manifest.permission.CAMERA},
                    MY_PERMISSIONS_REQUEST_CAMERA);
        }

    }

For your Manifest

<uses-permission android:name="android.permission.CAMERA"/>
Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • By the way, sometimes the Studio imports the wrong Manifest to your Activity. Just put android.Manifest.permission.CAMERA at the beginning if you encounter some problem – Rainmaker Nov 10 '17 at 17:20
  • I added permission but it not worked. In Android 6.0.1, when i take photo by back camera, the app has stopped. The photo is still available and saved in app's folder – Ngọc Anh Nov 11 '17 at 08:07
  • @NgọcAnh ok, if adding permission doesn't do it for you , then edit your question and put the log for us to see what the error actually is. There could be other reasons for it to crash – Rainmaker Nov 11 '17 at 08:39
  • I have added image of LogCat, i don't know if that is what you need. Please help me. Thank you so much – Ngọc Anh Nov 11 '17 at 16:24
  • @NgọcAnh your error should look like this https://www.google.ru/search?newwindow=1&biw=1920&bih=925&tbm=isch&sa=1&ei=pCwHWuXCNeyv6ASSrYCgDA&q=error+message+android+studio&oq=error+message+android+studio&gs_l=psy-ab.3...301.806.0.1187.4.4.0.0.0.0.310.310.3-1.1.0....0...1.1.64.psy-ab..3.0.0....0.EoGCImN7vII#imgrc=GxcSa1yMhnjSiM: Do you get anything similar with a word "error" or "exception"? – Rainmaker Nov 11 '17 at 17:01
  • I has fixed my error, maybe it has problem in sending file from main activity to other activity. Thanks for your support – Ngọc Anh Nov 13 '17 at 07:46