打开新活动然后片段转到根片段

时间:2016-09-02 05:40:11

标签: android android-fragments

我正在从片段打开whatsapp或相机等活动但是当我从当前活动回来时它会转到根片段而不是最近的片段。所以我需要为最近的片段使用另一个片段活动或其他任何解决方案?我经常搜索,但直到现在还没有得到适当的解决方案。

    private void galleryIntent()
        {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);//
            getActivity().startActivityForResult(Intent.createChooser(intent, "Select File"),SELECT_FILE);
        }

        private void cameraIntent()
        {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            getActivity().startActivityForResult(intent, REQUEST_CAMERA);
        }

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

            if (resultCode == Activity.RESULT_OK) {
                if (requestCode == SELECT_FILE)
                    onSelectFromGalleryResult(data);
                else if (requestCode == REQUEST_CAMERA)
                    onCaptureImageResult(data);
            }
        }

 private void onCaptureImageResult(Intent data) {
        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
        byteArray = bytes.toByteArray();

//        File destination = new File(Environment.getExternalStorageDirectory(), "dir/savings.csv");
//        if (!destination.exists()) {
//            destination.mkdirs();
//            destination.createNewFile();
//        }
//
//        File destination = new File(Environment.getExternalStorageDirectory()+File.separator+"Wish-A-Mitr"+File.separator,
//                System.currentTimeMillis() + ".jpg");


        File path = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES);
        File destination = new File(path, System.currentTimeMillis() +".jpg");


        FileOutputStream fo;
        try {
           if(path.exists())
           {
               path.mkdirs();
               destination.createNewFile();
           }

            fo = new FileOutputStream(destination);
            fo.write(bytes.toByteArray());
            fo.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        roundedImageView.setImageBitmap(thumbnail);
        // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
        Uri tempUri = getImageUri(getContext(), thumbnail);

        // CALL THIS METHOD TO GET THE ACTUAL PATH
        finalFile = new File(getRealPathFromURI(tempUri));

    }

    @SuppressWarnings("deprecation")
    private void onSelectFromGalleryResult(Intent data) {

        Bitmap bm=null;
        if (data != null) {
            try {
                bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                bm.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
                byteArray = bytes.toByteArray();

            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        roundedImageView.setImageBitmap(bm);

        // CALL THIS METHOD TO GET THE URI FROM THE BITMAP
        Uri tempUri = getImageUri(getContext(), bm);

        // CALL THIS METHOD TO GET THE ACTUAL PATH
         finalFile = new File(getRealPathFromURI(tempUri));

    }

    public Uri getImageUri(Context inContext, Bitmap inImage) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
        String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "imageFile", null);
        return Uri.parse(path);
    }

    public String getRealPathFromURI(Uri uri) {
        Cursor cursor = getContext().getContentResolver().query(uri, null, null, null, null);
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }

0 个答案:

没有答案