简体   繁体   中英

Starting Android Camera with Intent crashes

i am new in the programming for Android. And I get a inexplicable Exception in my App and i hope you can help me. The app covers the following Use-Case:

  1. The user press on the "take photo" Button
  2. The Google Camera will be open
  3. The Image will be save in the storage of the device
  4. The Path, where the image stored will be listet in a listview
  5. The user can click again on the button "take photo" (goto 2)

At firsttime the user can take photo succesful and the path will be show correctly in the app. But in case of clicking again the user can take a photo but the app crashes without a Exception when i want to save the image.

Scanning.java

package de.des;

import android.content.Intent;
import android.content.res.Configuration;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;


import java.util.ArrayList;
import java.util.List;

import de.auftrag.R;

public class Scanning extends AppCompatActivity {

    private List<String> pathlist;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_scanning);

        final ListView listview = (ListView) findViewById(R.id.listView);
        mimageView = (ImageView) this.findViewById(R.id.imageView);

        pathlist = new ArrayList<>();

        adapter = new ArrayAdapter<String>(this,R.layout.mylist, pathlist);

        listview.setAdapter(adapter);
    }


    private static final int TAKE_PHOTO_CODE = 1;
    public void takePhoto(View view) {

    File file = new File(Environment.getExternalStorageDirectory(), "fname_" +
            String.valueOf(System.currentTimeMillis()) + ".jpg");
    try {
        file.createNewFile();
        this.pathlist.add(file.getAbsolutePath());
        this.adapter.notifyDataSetChanged();
    } catch (IOException e) {
        Log.d("Scanning",e.getMessage());
    }

    Uri outputFileUri = Uri.fromFile(file);
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cameraIntent, 3);

        }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

}

activity_scanning.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:layout_width="match_parent"
android:layout_height="match_parent"
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="de.des.Scanning">

    <Button
        android:id="@+id/btnTakePhoto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/str_take_foto"
        android:onClick="takePhoto"/>

    <Button
        android:id="@+id/btnSelectFile"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/str_read_file"
        android:onClick="selectFile"
        android:layout_below="@+id/btnTakePhoto"
        android:layout_alignParentStart="true" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_below="@+id/btnSelectFile"
        android:layout_alignParentStart="true"
        android:layout_marginTop="73dp" />



</RelativeLayout>

AndroidMainifest.xml

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />


    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/logo"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name="de.des.Scanning"
            android:configChanges="orientation|screenSize"/>
        <activity android:name="de.des.ObjektdatenMap" />
    </application>
</manifest>

Best wishes Dominik

Try this:

private static final int TAKE_PHOTO_CODE = 1;
public void takePhoto(View view) {
    File file = new File(Environment.getExternalStorageDirectory(), "fname_" +
            String.valueOf(System.currentTimeMillis()) + ".jpg");
    Uri outputFileUri = Uri.fromFile(file);
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra("pic_path", file.getAbsolutePath());
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case TAKE_PHOTO_CODE:
            if(resultCode == Activity.RESULT_OK) {
                Uri imageUri = data.getData();
                String path = data.getExtras().getString("pic_path");
                pathlist.add(path);
                adapter.notifyDataSetChanged();
            }
            break;
    }
}

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