在活动中捕获,存储和检索照片

时间:2016-04-21 20:14:00

标签: java android image capture

我有一个问题,我无法解决。我今天几乎整天都在努力让这个工作起作用,但我不能和我有任何想法,所以决定寻求你的帮助。

另外,我已经浏览了google和stackoverflow以寻找答案,但这些都没有帮助我。

问题涉及用android捕捉照片。我有一个活动,我可以在其中“创建”一个以后存储在数据库中的新项目。我可以传递一个名称和位置,以及拍摄一张与之相关的照片。当我按“添加”时,它将保存到数据库中。一切都有效,除了相机意图。我希望能够在该活动中拍摄一张照片,该照片与我正在创建的“对象”绑定,所以稍后当我点击列表中的特定“对象”时,我可以查看该照片。我的AndroidManifest中有权限(写入和读取),但他们仍然没有帮助。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
                 android:maxSdkVersion="18"
    />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
                 android:maxSdkVersion="18"
    />
<uses-feature android:name="android.hardware.camera"
              android:required="false"
    />

我的Android版本是6.0。 此代码是我收集的最新解决方案汇编,旨在使其工作。我非常感谢你对我的问题的帮助。

import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class CreateItemFragment extends Fragment {

    private static final int REQUEST_PHOTO = 2;
    private static final String TAG = "button clicked";
    private static final int REQUEST_EXTERNAL_STORAGE = 1;
    private static String[] PERMISSIONS_STORAGE = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };

    private TextView mNameText;
    private TextView mLocationText;
    private Button mAddButton;
    private ImageButton mPhotoButton;
    private ImageView mPhotoView;
    private String mCurrentPhotoPath;
    private File mPhotoFile;
    private Item mItem;
    private List<Item> mItems;
    private static ItemStash sItemStash;

    public static CreateItemFragment newInstance(){
        return new CreateItemFragment();
    }

    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_create_item, container, false);

        mNameText = (TextView)view.findViewById(R.id.item_name);
        mLocationText = (TextView)view.findViewById(R.id.item_location);
        sItemStash = sItemStash.get(getContext());


        mAddButton = (Button)view.findViewById(R.id.add_button);
        mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mNameText.getText().length() > 0 && mLocationText.getText().length() > 0){
                    sItemStash.addItem(
                            new Item(mNameText.getText().toString(),
                                    mLocationText.getText().toString()));
                    Toast.makeText(getActivity(), mNameText.getText().toString() + " added to the list!", Toast.LENGTH_SHORT).show();
                }
                mNameText.setText("");
                mLocationText.setText("");

            }
        });

        mPhotoButton = (ImageButton)view.findViewById(R.id.photo_button);
        //final Intent captureImage = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        /*
        PackageManager packageManager = getActivity().getPackageManager();
        boolean canTakePhoto = mPhotoFile != null && captureImage.resolveActivity(packageManager) != null;
        mPhotoButton.setEnabled(canTakePhoto);

        if(canTakePhoto) {
            Uri uri = Uri.fromFile(mPhotoFile);
            captureImage.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        }
        */

        mPhotoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                verifyStoragePermissions(getActivity());
                capturePhotoIntent();
                //startActivityForResult(captureImage, REQUEST_PHOTO);
                //mPhotoFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString());
                //mPhotoFile = new File(mItem.setPhotoFilename(mNameText.getText().toString()));
            }


        });

        mPhotoView = (ImageView)view.findViewById(R.id.item_photo);
        //updatePhotoView();

        return view;
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data){
        if(requestCode == REQUEST_PHOTO && resultCode == Activity.RESULT_OK){
            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap)extras.get("data");
            //mPhotoFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString());
            mPhotoView.setImageBitmap(imageBitmap);
        }


    }

    /*
    private void updatePhotoView(){
        if(mPhotoFile == null || !mPhotoFile.exists()){
            Log.d(TAG, "is clicked");
            mPhotoView.setImageDrawable(null);
        }else{
            Bitmap bitmap = PhotoUtils.getScaledBitmap(mPhotoFile.getPath(), getActivity());
            mPhotoView.setImageBitmap(bitmap);
        }
    }
    */

    private void capturePhotoIntent(){
        Intent photoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        PackageManager packageManager = getActivity().getPackageManager();
        if(photoIntent.resolveActivity(packageManager) != null){
            File photoFile = null;
            try{
                photoFile = createImageFile();
            }catch (IOException ioe){
                ioe.printStackTrace();
            }

            if(photoFile != null) {
                photoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
                startActivityForResult(photoIntent, REQUEST_PHOTO);
            }

        }
    }

    public File createImageFile() throws IOException {
        verifyStoragePermissions(getActivity());

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,
                ".jpg",
                storageDir
        );

        mCurrentPhotoPath = "file:" + image.getAbsolutePath();
        return image;
    }

    public static void verifyStoragePermissions(Activity activity) {
        int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

        if (permission != PackageManager.PERMISSION_GRANTED) {

            ActivityCompat.requestPermissions(
                    activity,
                    PERMISSIONS_STORAGE,
                    REQUEST_EXTERNAL_STORAGE
            );
        }
    }







}

*编辑 忘了粘贴我的堆栈跟踪。我知道了:

04-21 22:34:56.688 3574-3604/com.bignerdranch.android.tingleapp 

W/OpenGLRenderer: Fail to change FontRenderer cache size, it already initialized
04-21 22:34:56.704 3574-3574/com.bignerdranch.android.tingleapp D/android.widget.GridLayout: horizontal constraints: x1-x0>=163, x2-x1>=1080, x2-x0<=1080 are inconsistent; permanently removing: x2-x0<=1080. 
04-21 22:34:56.708 3574-3574/com.bignerdranch.android.tingleapp D/android.widget.GridLayout: vertical constraints: y2-y0>=745, y2-y1<=136, y1-y0<=136 are inconsistent; permanently removing: y2-y1<=136. 
04-21 22:34:56.819 3574-3604/com.bignerdranch.android.tingleapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb8767340
04-21 22:34:57.533 3574-3574/com.bignerdranch.android.tingleapp W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at java.io.File.createNewFile(File.java:939)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at java.io.File.createTempFile(File.java:1004)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at com.bignerdranch.android.tingleapp.CreateItemFragment.createImageFile(CreateItemFragment.java:173)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at com.bignerdranch.android.tingleapp.CreateItemFragment.capturePhotoIntent(CreateItemFragment.java:154)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at com.bignerdranch.android.tingleapp.CreateItemFragment.access$300(CreateItemFragment.java:30)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at com.bignerdranch.android.tingleapp.CreateItemFragment$2.onClick(CreateItemFragment.java:108)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at android.view.View.performClick(View.java:5226)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at android.view.View$PerformClick.run(View.java:21266)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at android.os.Looper.loop(Looper.java:168)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5845)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
04-21 22:34:57.538 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at libcore.io.Posix.open(Native Method)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err:     at java.io.File.createNewFile(File.java:932)
04-21 22:34:57.539 3574-3574/com.bignerdranch.android.tingleapp W/System.err:   ... 14 more

方法verisfyStoragePermission()应该修复我在其他主题中发现的错误,但我仍然会收到此错误

0 个答案:

没有答案