0

我有fragment一个画布绘图并将其保存到外部存储器。我通过连接 USB 并搜索文件目录进入设备。我在Android/data/appname/files/img/nameofimage.png. 现在我有一个第二个片段,它在相机拍摄时保存照片,但我找不到它们。

相机

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());
                    }
                }
            }
        }
    }

帆布

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());
                                }

点击

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);
        }

    }

我不完全理解代码,为什么要保存到不同的位置。另外,我需要做什么才能将其保存下来Android/data/appname/files/Camera/

编辑

logcat 告诉我它被保存在这里:06-08 16:21:49.333: D/FILE(4818): 图像文件的目标是:/storage/emulated/0/Camera/image.jpg我假设它被列为在文件中是私有的,这就是我找不到它的原因。这并没有告诉我为什么它被保存在这里。

第二次编辑

当前代码

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");
        }
    }
4

1 回答 1

0

试试这个代码

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);
}

请记住 ,您需要在 Manifest.xml 中使用此权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2013-06-08T21:48:46.237 回答