Android:将相机图片保存到错误的地方?

时间:2013-06-08 21:31:57

标签: android android-camera android-external-storage

我有一个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());
                                }

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

    }

我不完全理解代码,为什么这个保存到不同的位置。另外,我需要做些什么才能将其保存在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");
        }
    }

1 个答案:

答案 0 :(得分: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" />