拍摄相机照片的onActvitiyResult后片段不显示

时间:2015-07-03 09:43:31

标签: android android-fragments android-camera android-camera-intent

我已经从我的PhotosFragment使用startActivityForResult()启动了相机,并使用适配器解码图像并在片段上的gridview上显示。它捕获正确调用的图像和onActvityResult,并且onActvityResult上的所有代码都能正常工作(通过调试找到)。但是我需要在片段上的gridview上显示摄像头拍摄的图像。拍照并保存后,进入Photofragment onActivityResult,然后转到直接在home片段(我在MainActivity上加载了HomeFragment和PhotosFragment)。没有显示它仅在Samusng S4设备上显示。

我在manifest中试过了Actvity的configChanged参数。对我不起作用。

参考:

Android: Activity getting Destroyed after calling Camera Intent

https://stackoverflow.com/a/10411504

PhotosFragment.java

private void selectImage() {
        final CharSequence[] items = {"Take Photo", "Choose from Library",
                "Cancel"};

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("Add Photo");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals("Take Photo")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File f = new File(android.os.Environment
                            .getExternalStorageDirectory(), "temp.jpg");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
                    startActivityForResult(intent, REQUEST_CAMERA);
                } else if (items[item].equals("Choose from Library")) {
                    Intent intent = new Intent(
                            Intent.ACTION_PICK,
                            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                    intent.setType("image/*");
                    startActivityForResult(
                            Intent.createChooser(intent, "Select File"),
                            SELECT_FILE);
                } else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }



    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        //super.onActivityResult(requestCode,resultCode,data);
        Log.i("CameraResult","requestCode->"+requestCode+"where REQUEST_CAMERA->"+REQUEST_CAMERA);
        Log.i("CameraResult","resultCode->"+resultCode+"where getActivity().RESULT_OK->"+getActivity().RESULT_OK);
        Log.i("CameraResult","data->"+data);

        if (resultCode == getActivity().RESULT_OK) {
            int thumbFactor = 6; // choose a power of 2

            if (requestCode == REQUEST_CAMERA) {
                File f = new File(Environment.getExternalStorageDirectory()
                        .toString());
                for (File temp : f.listFiles()) {
                    if (temp.getName().equals("temp.jpg")) {
                        f = temp;
                        break;
                    }
                }

                Log.i("CameraResult","f.getAbsolutePath()->"+f.getAbsolutePath());
                try {

                    imagePaths.add(f.getAbsolutePath());

                    //CarPicturesAdapter adapter = new CarPicturesAdapter(getActivity().getBaseContext(), R.id.pictureGrid, images);

                    CarPicturesAdapter adapter = new CarPicturesAdapter(getActivity().getBaseContext(), R.id.pictureGrid, imagePaths);
                    contentImage.setAdapter(adapter);

                    Log.i("CameraResult", "set adapter");

                    f.delete();

                    Log.i("CameraResult", "file deleted");
                } catch (Exception e) {
                    e.printStackTrace();
                }

                //super.onActivityResult(requestCode,resultCode,data);
            }
            /*else{
                super.onActivityResult(requestCode,resultCode,data);
            }*/
        }
    }

CarPicturesAdapter.java

public class CarPicturesAdapter extends ArrayAdapter<String>{

    private SparseBooleanArray mSelectedItemsIds;
    private final ArrayList<String> objects;
    private Context context;
    private String[] images;

    public CarPicturesAdapter(Context context, int textViewResourceId,
                           ArrayList<String> objects) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.images = new String[objects.size()];
        objects.toArray(this.images);
        this.objects = objects;
        mSelectedItemsIds = new SparseBooleanArray();

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        CheckableLayout checkableLayout = new CheckableLayout(context);
        View rowView = inflater.inflate(R.layout.picture_item_gridview, parent, false);

        SquareImageView imageView = (SquareImageView) rowView.findViewById(R.id.car_image);

        //imageView.setImageBitmap(Image.getBitmap(images[position]));

        ImageLoader.getInstance().displayImage(String.valueOf(Uri.fromFile(new File(images[position]))), imageView);

        checkableLayout.addView(rowView);
        return checkableLayout;
    }

    @Override
    public void remove(String image) {
        objects.remove(image);
        notifyDataSetChanged();
    }

    public void toggleSelection(int position) {
        selectView(position, !mSelectedItemsIds.get(position));
    }

    public void removeSelection() {
        mSelectedItemsIds = new SparseBooleanArray();
        notifyDataSetChanged();
    }

    public void selectView(int position, boolean value) {
        if (value)
            mSelectedItemsIds.put(position, value);
        else
            mSelectedItemsIds.delete(position);
        notifyDataSetChanged();
    }

    public int getSelectedCount() {
        return mSelectedItemsIds.size();
    }

    public SparseBooleanArray getSelectedIds() {
        return mSelectedItemsIds;
    }

    public class CheckableLayout extends FrameLayout implements Checkable {
        private boolean mChecked;

        public CheckableLayout(Context context) {
            super(context);
        }

        @SuppressWarnings("deprecation")
        public void setChecked(boolean checked) {
            mChecked = checked;
            setBackgroundDrawable(checked ? getResources().getDrawable(
                    android.R.color.holo_blue_dark) : null);
        }

        public boolean isChecked() {
            return mChecked;
        }

        public void toggle() {
            setChecked(!mChecked);
        }

    }
}

0 个答案:

没有答案