博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android初学第72天
阅读量:5132 次
发布时间:2019-06-13

本文共 9903 字,大约阅读时间需要 33 分钟。

Android初学第72天

16_CameraIntent

代码

Criminalintent

CrimeFragment.java

package com.bignerdranch.android.criminalintent;import android.app.Activity;import android.content.Intent;import android.content.pm.PackageManager;import android.content.pm.ResolveInfo;import android.database.Cursor;import android.graphics.Bitmap;import android.net.Uri;import android.os.Bundle;import android.provider.ContactsContract;import android.provider.MediaStore;import android.support.v4.app.Fragment;import android.support.v4.app.FragmentManager;import android.support.v4.content.FileProvider;import android.text.Editable;import android.text.TextWatcher;import android.text.format.DateFormat;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.Button;import android.widget.CheckBox;import android.widget.CompoundButton;import android.widget.EditText;import android.widget.ImageButton;import android.widget.ImageView;import java.io.File;import java.util.Date;import java.util.List;import java.util.UUID;import static android.widget.CompoundButton.*;public class CrimeFragment extends Fragment {    private static final String ARG_CRIME_ID = "crime_id";    private static final String DIALOG_DATE = "DialogDate";    private static final int REQUEST_DATE = 0;    private static final int REQUEST_CONTACT = 1;    private static final int REQUEST_PHOTO = 2;    private Crime mCrime;    private File mPhotoFile;    private EditText mTitleField;    private Button mDateButton;    private CheckBox mSolvedCheckbox;    private Button mReportButton;    private Button mSuspectButton;    private ImageButton mPhotoButton;    private ImageView mPhotoView;    public static CrimeFragment newInstance(UUID crimeId) {        Bundle args = new Bundle();        args.putSerializable(ARG_CRIME_ID, crimeId);        CrimeFragment fragment = new CrimeFragment();        fragment.setArguments(args);        return fragment;    }    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        UUID crimeId = (UUID) getArguments().getSerializable(ARG_CRIME_ID);        mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);        mPhotoFile = CrimeLab.get(getActivity()).getPhotoFile(mCrime);    }    @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,                             Bundle savedInstanceState) {        View v = inflater.inflate(R.layout.fragment_crime, container, false);        mTitleField = (EditText) v.findViewById(R.id.crime_title);        mTitleField.setText(mCrime.getTitle());        mTitleField.addTextChangedListener(new TextWatcher() {            @Override            public void beforeTextChanged(CharSequence s, int start, int count, int after) {            }            @Override            public void onTextChanged(CharSequence s, int start, int before, int count) {                mCrime.setTitle(s.toString());            }            @Override            public void afterTextChanged(Editable s) {            }        });        mDateButton = (Button) v.findViewById(R.id.crime_date);        updateDate();        mDateButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                FragmentManager manager = getFragmentManager();                DatePickerFragment dialog = DatePickerFragment                        .newInstance(mCrime.getDate());                dialog.setTargetFragment(CrimeFragment.this, REQUEST_DATE);                dialog.show(manager, DIALOG_DATE);            }        });        mSolvedCheckbox = (CheckBox) v.findViewById(R.id.crime_solved);        mSolvedCheckbox.setChecked(mCrime.isSolved());        mSolvedCheckbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override            public void onCheckedChanged(CompoundButton buttonView,                     boolean isChecked) {                mCrime.setSolved(isChecked);            }        });        mReportButton = (Button) v.findViewById(R.id.crime_report);        mReportButton.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                Intent i = new Intent(Intent.ACTION_SEND);                i.setType("text/plain");                i.putExtra(Intent.EXTRA_TEXT, getCrimeReport());                i.putExtra(Intent.EXTRA_SUBJECT,                        getString(R.string.crime_report_subject));                i = Intent.createChooser(i, getString(R.string.send_report));                startActivity(i);            }        });        final Intent pickContact = new Intent(Intent.ACTION_PICK,                ContactsContract.Contacts.CONTENT_URI);        mSuspectButton = (Button) v.findViewById(R.id.crime_suspect);        mSuspectButton.setOnClickListener(new View.OnClickListener() {            public void onClick(View v) {                startActivityForResult(pickContact, REQUEST_CONTACT);            }        });        if (mCrime.getSuspect() != null) {            mSuspectButton.setText(mCrime.getSuspect());        }        PackageManager packageManager = getActivity().getPackageManager();        if (packageManager.resolveActivity(pickContact,                PackageManager.MATCH_DEFAULT_ONLY) == null) {            mSuspectButton.setEnabled(false);        }        mPhotoButton = (ImageButton) v.findViewById(R.id.crime_camera);        final Intent captureImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        boolean canTakePhoto = mPhotoFile != null &&                captureImage.resolveActivity(packageManager) != null;        mPhotoButton.setEnabled(canTakePhoto);        mPhotoButton.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Uri uri = FileProvider.getUriForFile(getActivity(),                        "com.bignerdranch.android.criminalintent.fileprovider",                        mPhotoFile);                captureImage.putExtra(MediaStore.EXTRA_OUTPUT, uri);                List
cameraActivities = getActivity() .getPackageManager().queryIntentActivities(captureImage, PackageManager.MATCH_DEFAULT_ONLY); for (ResolveInfo activity : cameraActivities) { getActivity().grantUriPermission(activity.activityInfo.packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION); } startActivityForResult(captureImage, REQUEST_PHOTO); } }); mPhotoView = (ImageView) v.findViewById(R.id.crime_photo); updatePhotoView(); return v; } @Override public void onPause() { super.onPause(); CrimeLab.get(getActivity()) .updateCrime(mCrime); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != Activity.RESULT_OK) { return; } if (requestCode == REQUEST_DATE) { Date date = (Date) data .getSerializableExtra(DatePickerFragment.EXTRA_DATE); mCrime.setDate(date); updateDate(); } else if (requestCode == REQUEST_CONTACT && data != null) { Uri contactUri = data.getData(); // Specify which fields you want your query to return // values for. String[] queryFields = new String[]{ ContactsContract.Contacts.DISPLAY_NAME }; // Perform your query - the contactUri is like a "where" // clause here Cursor c = getActivity().getContentResolver() .query(contactUri, queryFields, null, null, null); try { // Double-check that you actually got results if (c.getCount() == 0) { return; } // Pull out the first column of the first row of data - // that is your suspect's name. c.moveToFirst(); String suspect = c.getString(0); mCrime.setSuspect(suspect); mSuspectButton.setText(suspect); } finally { c.close(); } } else if (requestCode == REQUEST_PHOTO) { Uri uri = FileProvider.getUriForFile(getActivity(), "com.bignerdranch.android.criminalintent.fileprovider", mPhotoFile); getActivity().revokeUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION); updatePhotoView(); } } private void updateDate() { mDateButton.setText(mCrime.getDate().toString()); } private String getCrimeReport() { String solvedString = null; if (mCrime.isSolved()) { solvedString = getString(R.string.crime_report_solved); } else { solvedString = getString(R.string.crime_report_unsolved); } String dateFormat = "EEE, MMM dd"; String dateString = DateFormat.format(dateFormat, mCrime.getDate()).toString(); String suspect = mCrime.getSuspect(); if (suspect == null) { suspect = getString(R.string.crime_report_no_suspect); } else { suspect = getString(R.string.crime_report_suspect, suspect); } String report = getString(R.string.crime_report, mCrime.getTitle(), dateString, solvedString, suspect); return report; } private void updatePhotoView() { if (mPhotoFile == null || !mPhotoFile.exists()) { mPhotoView.setImageDrawable(null); } else { Bitmap bitmap = PictureUtils.getScaledBitmap( mPhotoFile.getPath(), getActivity()); mPhotoView.setImageBitmap(bitmap); } }}

转载于:https://www.cnblogs.com/zhouheng0918/p/9215121.html

你可能感兴趣的文章
深入理解JVM-类加载器深入解析(1)
查看>>
数字在排序数组中出现的次数
查看>>
PhotoShop中画圆角矩形最简单方法(图文并茂)!
查看>>
java容器类---概述
查看>>
Mahout-Pearson correlation的实现
查看>>
POJ--1300--Door Man【推断无向图欧拉通路】
查看>>
ostringstream使用
查看>>
房费制——报表(1)
查看>>
前端开发之初探五
查看>>
pycharm 文件修改的星号(*)提示||文件是否修改过
查看>>
intel simd 资料
查看>>
OC与Swift混编
查看>>
【例9.7】友好城市
查看>>
暑假第五测
查看>>
VBA Mysql 类
查看>>
s:radio
查看>>
STM32-内存管理
查看>>
Asp.net MVC Pager分页实现
查看>>
Palindrome Function
查看>>
反射复习笔记01
查看>>