简体   繁体   中英

Unable to retrieve captured image from camera

I am building a simple camera application in android, which would launch a camera intent on click of a button. The camera intent would then click an image which would later be displayed in the ImageView present in the MainActivity. The issue however is that I am able to capture the image but as soon as the image is captured instead of displaying the image the app suddenly crashes.

Here is my code for the app:

MainActivity.java

package com.example.suzancruz.cameraapplicaton;

import android.content.Intent;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;


public class MainActivity extends AppCompatActivity {


    static final int REQUEST_IMAGE_CAPTURE = 123;
    ImageView myImageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    }

    //Launching the camera
    public void launchCamera(View view){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        //Take a picture and pass results along to onActivityResult
        startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
    }

    //If you want to return the image taken
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK){
            //Get the photo
            Bundle extras = data.getExtras();
            Bitmap photo = (Bitmap) extras.get("data");
            myImageView.setImageBitmap(photo);
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.suzancruz.cameraapplicaton.MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="launchCamera"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Take Picture"

        />
    <ImageView
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:id="@+id/myImageView"

        />
</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.suzancruz.cameraapplicaton">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <uses-feature android:name="android.hardware.camera2"></uses-feature>
        <uses-permission android:name="android.permission.CAMERA"/>
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    </application>

</manifest>

try this `public void onTakeImg() {

    values = new ContentValues();
    values.put(MediaStore.Images.Media.TITLE, "Image");
    values.put(MediaStore.Images.Media.DESCRIPTION, "From your Camera");
    imageUri = getActivity().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

}
   public void onActivityResult(int requestCode, int resultCode, Intent data) {

    switch (requestCode) {
        case REQUEST_IMAGE_CAPTURE:
            if (resultCode == RESULT_OK) {
                try {
                    mBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), imageUri);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                onImageAvailable(mBitmap, requestCode);
                Img.setImageBitmap(mBitmap);
                               }
            break;

    }
}

void onImageAvailable(Bitmap imageBitmap, int requestCode) {
    String output = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + "/.immigration/temp/";
    File f = new File(output);
    if (!f.exists()) {
        f.mkdirs();
    }

    String filename = output + "Image.jpg";

    File fileDelete = new File(filename);

    if (fileDelete.exists()) {
        if (fileDelete.delete()) {
            System.out.println("file Deleted :" + filename);
        } else {
            System.out.println("file not Deleted :" + filename);
        }
    }

    try {
        FileOutputStream out = new FileOutputStream(filename);
        Matrix matrix = new Matrix();
        if (imageBitmap.getWidth() < imageBitmap.getHeight()) {
            matrix.postRotate(90);
        }
        mBitmap = Bitmap.createBitmap(imageBitmap, 0, 0, imageBitmap.getWidth(), imageBitmap.getHeight(), matrix, true);
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
        out.close();
        Log.d("Ar", "Ole");
    } catch (IOException e) {
        e.printStackTrace();
        Log.d("Ar", e.toString());
    }

    File fdelete = new File(imageUri.getPath());
    if (fdelete.exists()) {
        if (fdelete.delete()) {
            System.out.println("file Deleted :" + imageUri.getPath());
        } else {
            System.out.println("file not Deleted :" + imageUri.getPath());
        }
    }

}`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM