繁体   English   中英

拍照后相机未保存

[英]Camera is not saving after taking picture

我可以按一个按钮,打开原生相机应用程序,然后成功拍照。 但是当我在手机上查看图库或照片原生应用时,图片不会保存在那里。 我对Android很新,所以很可能我在代码中遗漏了一些重要内容。

问题:

1)这些图片在哪里保存?

2)我可以以某种方式修改以下代码以保存到内部存储,因此使用我的应用程序拍摄的所有照片都是私密的,只能通过我的应用程序访问吗?

3)如果我想将这些图片保存到一个对象,以及一些文本/其他输入,那么最好的方法是什么? 我应该保存一个Uri或一些标识符以便稍后引用该图像,还是保存实际的BitMap图像?

非常感谢任何帮助,谢谢!

这是我拍摄照片的代码:

mImageButton.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View v)
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        imageUri = CameraUtils.getOutputMediaFileUri(CameraUtils.MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, REQUEST_IMAGE);
    }
}

CameraUtils课程直接来自Google开发人员指南

public static Uri getOutputMediaFileUri(int type)
{
    return Uri.fromFile(getOutputMediaFile(type));
}

public static File getOutputMediaFile(int type)
{
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "camera");

    if (!mediaStorageDir.exists())
    {
        if (!mediaStorageDir.mkdirs())
        {
            return null;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE)
    {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "IMG_" + timeStamp + ".jpg");
    }
    else if(type == MEDIA_TYPE_VIDEO)
    {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "VID_" + timeStamp + ".mp4");
    }
    else
    {
        return null;
    }

    return mediaFile;
}

1)通过查看代码,我希望将图片保存在名为“camera”的目录中,该目录可以在设备的Pictures文件夹中找到(外部存储器)。 这些可能不会立即出现在您的图库中,但是在Android的更高版本中(Kitkat和果冻豆虽然我现在无法验证)您应该能够打开照片应用并在那里找到它们。 如果不是这样,则启动文件浏览器应用程序(示例应用程序是ASTRO文件管理器或X-Plore)并浏览到您应该看到图像的图片/相机目录。 下次您的媒体被重新编入索引(手机重启或从其他地方触发重新索引)时,您应该在您的图库/照片应用中看到这些图片。 如果你想以编程方式刷新你的媒体, 这里可能有帮助。 最后,确保你有READ_EXTERNAL_STORAGE许可,您的Android清单作为指定 (Android的文档)。

2)如果要将图像保存为仅适用于您的应用程序,则需要将它们保存到应用程序的内部数据目录中。 从Android文档中直接看一下这个 确保使用MODE_PRIVATE标志。

3)为此,您需要将文件路径存储在应用程序可访问的位置。 您可以将文件路径保存为包含其他文本数据的文本文件,也可以使用sqlite数据库。 最后,您可以使用ORMLite for Android这样的ORM来保存一个java对象,该对象可能包含您的图片数据并且有一些您想要保留的字段(标题,描述,路径等)。 这里这里是如何在Android中开始使用SQLite数据库的介绍(直接来自官方文档)。 如果你想使用ORMLite,有大量的信息在其网站上这里 开发人员花了很多时间回答StackOverflow问题。

您可以通过一些简单的Google搜索来回答所有问题。 它们是Android中非常标准和基本的事情,因此您应该能够在线找到大量的信息和教程。

编辑

回应您对第二个问题的评论。 这是我可能会做的(或类似的东西):

请注意,我没有测试这个。 这是我的头脑。 如果您有更多问题在这里发表评论!

活动代码......

mImageButton.setOnClickListener(new View.OnClickListener()
{
    public void onClick(View v)
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        imageUri = CameraUtils.getOutputMediaFileUri(currentActivity, CameraUtils.MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, REQUEST_IMAGE);
    }
}

public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_IMAGE)
    {
        if (resultCode == RESULT_OK)
        {
            String pathToInternallyStoredImage = CameraUtils.saveToInternalStorage(this, imageUri);
            // Load the bitmap from the path and display it somewhere, or whatever
        }
        else if (resultCode == RESULT_CANCELED)
        {
            //Cancel code
        }
    }
}

CameraUtils类代码......

public static Uri getOutputMediaFileUri(int type)
{
    return Uri.fromFile(getOutputMediaFile(type));
}

public static File getOutputMediaFile(int type)
{
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "camera");

    createMediaStorageDir(mediaStorageDir);

    return createFile(type, mediaStorageDir);
}

private static File getOutputInternalMediaFile(Context context, int type)
{
    File mediaStorageDir = new File(context.getFilesDir(), "myInternalPicturesDir");

    createMediaStorageDir(mediaStorageDir);

    return createFile(type, mediaStorageDir);
}

private static void createMediaStorageDir(File mediaStorageDir) // Used to be 'private void ...'
{
    if (!mediaStorageDir.exists())
    {
        mediaStorageDir.mkdirs(); // Used to be 'mediaStorage.mkdirs();'
    }
} // Was flipped the other way

private static File createFile(int type, File mediaStorageDir ) // Used to be 'private File ...'
{
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    File mediaFile = null;
    if (type == MEDIA_TYPE_IMAGE)
    {
        mediaFile = new File(mediaStorageDir .getPath() + File.separator +
                "IMG_" + timeStamp + ".jpg");
    }
    else if(type == MEDIA_TYPE_VIDEO)
    {
        mediaFile = new File(mediaStorageDir .getPath() + File.separator +
                "VID_" + timeStamp + ".mp4");
    }
    return mediaFile;
}

public static String saveToInternalStorage(Context context, Uri tempUri)
{
    InputStream in = null;
    OutputStream out = null;

    File sourceExternalImageFile = new File(tempUri.getPath());
    File destinationInternalImageFile = new File(getOutputInternalMediaFile(context).getPath());

    try
    {
        destinationInternalImageFile.createNewFile();

        in = new FileInputStream(sourceExternalImageFile);
        out = new FileOutputStream(destinationInternalImageFile);

        // Transfer bytes from in to out
        byte[] buf = new byte[1024];
        int len;
        while ((len = in.read(buf)) > 0)
        {
            out.write(buf, 0, len);
        }
    }
    catch (IOException e)
    {
        e.printStackTrace();
        //Handle error
    }
    finally
    {
        try {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                in.close();
            }
        } catch (IOException e) {
            // Eh
        }
    }
    return destinationInternalImageFile.getPath();
}

所以现在你有了指向你内部存储的图像的路径,然后你可以从onActivityResult操作/加载它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM