使用Intent启动Android相机崩溃

时间:2016-06-11 19:56:49

标签: java android

我是Android的编程新手。我在我的应用程序中得到了一个莫名其妙的异常,我希望你能帮助我。该应用程序涵盖以下用例:

  1. 用户按下“拍照”按钮
  2. Google相机将会打开
  3. 图像将保存在设备的存储中
  4. 路径,存储的图像将在列表视图中进行监听
  5. 用户可以再次点击“拍照”(转到2)
  6. 按钮

    首次用户可以拍照成功,路径将在应用中正确显示。但是如果再次点击,用户可以拍照,但当我想要保存图像时应用程序崩溃而没有例外。

    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>
    

    祝福 多米尼克

1 个答案:

答案 0 :(得分:0)

试试这个:

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;
    }
}