سؤال

I have a fragment that takes a canvas drawing and saves it to external memory. I go into the device by connecting the USB and searching the file directory. I find it under Android/data/appname/files/img/nameofimage.png. Now I have a 2nd fragment that is saving pictures when the camera takes them but I can't find them.

Camera

public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check that request code matches ours:
        if (requestCode == CALL_BACK) {
            // Check if your application folder exists in the external storage,
            // if not create it:
            File imageStorageFolder = new File(
                    Environment.getExternalStorageDirectory() + File.separator
                            + "Camera");
            if (!imageStorageFolder.exists()) {
                imageStorageFolder.mkdirs();
                Log.d("FILE",
                        "Folder created at: " + imageStorageFolder.toString());
            }

            // Check if data in not null and extract the Bitmap:
            if (data != null) {
                String filename = "image";
                String fileNameExtension = ".jpg";
                File sdCard = Environment.getExternalStorageDirectory();
                String imageStorageFolderName = File.separator + "Camera"
                        + File.separator;
                File destinationFile = new File(sdCard, imageStorageFolderName
                        + filename + fileNameExtension);
                Log.d("FILE", "the destination for image file is: "
                        + destinationFile);
                if (data.getExtras() != null) {
                    Bitmap bitmap = (Bitmap) data.getExtras().get("data");
                    try {
                        FileOutputStream out = new FileOutputStream(
                                destinationFile);
                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                        out.flush();
                        out.close();
                    } catch (Exception e) {
                        Log.e("FILE", "ERROR:" + e.toString());
                    }
                }
            }
        }
    }

Canvas

capSig.setView(sign = new Sign(this.getActivity(), null))
                .setMessage(R.string.store_question)
                .setPositiveButton(R.string.save,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {
                                try {
                                    sign.setDrawingCacheEnabled(true);
                                    sign.getDrawingCache()
                                            .compress(
                                                    Bitmap.CompressFormat.PNG,
                                                    10,
                                                    new FileOutputStream(
                                                            new File(
                                                                    getActivity()
                                                                            .getExternalFilesDir(
                                                                                    "img"),
                                                                    "signature.png")));
                                } catch (Exception e) {
                                    Log.e("Error ", e.toString());
                                }

onClick

private class ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
            startActivityForResult(intent, CALL_BACK);
        }

    }

I don't fully understand the code, why is this saving to a different location. Also what would I need to do to get it to save under Android/data/appname/files/Camera/ ?

edit

the logcat tells me its being saved here: 06-08 16:21:49.333: D/FILE(4818): the destination for image file is: /storage/emulated/0/Camera/image.jpg Which I assume is listed as private in the files and that's why I cant find it. This does not tell me why its being saved here though.

2nd Edit

Current Code

private class ClickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

            File sdCard = Environment.getExternalStorageDirectory();
            String path = sdCard.getAbsolutePath() + "/Camera"  ;

            File dir = new File(path);
            if (!dir.exists()) {
                if (dir.mkdirs()) {

                }
            }
            String FileName = "image";
            File file = new File(path, FileName + ".jpg");
            Uri outputFileUri = Uri.fromFile(file);
            intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
            startActivityForResult(intent, CALL_BACK);
        }

    }

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // Check that request code matches ours:
        if (requestCode == CALL_BACK) {         
            Log.v("RESULT", "Picture Taken");
        }
    }
هل كانت مفيدة؟

المحلول

Try this code

private void TakePhoto() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    File sdCard = Environment.getExternalStorageDirectory();
    String path = sdCard.getAbsolutePath() + "/Camera"  ;

    File dir = new File(path);
    if (!dir.exists()) {
        if (dir.mkdirs()) {

        }
    }
    String FileName = "image";
    File file = new File(path, FileName + ".jpg");
    Uri outputFileUri = Uri.fromFile(file);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
    startActivityForResult(intent, TAKE_PICTURE);
}

Remember You need this Permission in your Manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top