相机拍照后不保存 [英] Camera is not saving after taking picture

查看:257
本文介绍了相机拍照后不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以preSS按钮,打开原生相机应用,并成功地拍照。但后来当我检查我的手机上的照片或照片原生应用,图像不会被保存在那里。我很新到Android因此很可能我的思念在我的code一些重要的东西。

问题:

1)凡这些图片被保存?

2)我可以修改低于code莫名其妙地保存,而不是内部存储器,所以用我的应用程序拍摄的所有照片都经过我的应用程序私有的,只能访问?

3)如果我想将这些照片保存到一个对象,具有一定的文字/其它输入一起,这将是最好的方法是什么?如果我只是节省了乌里或某些标识以后引用图像,或保存实际的BitMap 形象?

任何帮助是极大AP preciated,谢谢!

下面是我的code拍摄照片:

  mImageButton.setOnClickListener(新View.OnClickListener()
{
    公共无效的onClick(视图v)
    {
        意向意图=新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
        imageUri = CameraUtils.getOutputMediaFileUri(CameraUtils.MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
        startActivityForResult(意向,REQUEST_IMAGE);
    }
}

从的 CameraUtils 类>谷歌开发者指南

 公共静态乌里getOutputMediaFileUri(整型)
{
    返回Uri.fromFile(getOutputMediaFile(类型));
}公共静态文件getOutputMediaFile(整型)
{
    文件mediaStorageDir =新的文件(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES),照相机);    如果(!mediaStorageDir.exists())
    {
        如果(!mediaStorageDir.mkdirs())
        {
            返回null;
        }
    }    字符串的timeStamp =新的SimpleDateFormat(YYYYMMDD_HHMMSS)格式(新的Date());
    文件媒体文件;
    如果(类型== MEDIA_TYPE_IMAGE)
    {
        媒体文件=新的文件(mediaStorageDir.getPath()+文件分割符+
                IMG_+ +的timeStamp.JPG);
    }
    否则,如果(类型== MEDIA_TYPE_VIDEO)
    {
        媒体文件=新的文件(mediaStorageDir.getPath()+文件分割符+
                VID_+的timeStamp +.MP4);
    }
    其他
    {
        返回null;
    }    返回媒体文件;
}


解决方案

1)通过查看code,我期望的图片保存在一个名为'摄像头'目录这将在中找到图片您的设备(外置存储)文件夹中。这些可能不会立即出现在您的画廊,但是在后来的Andr​​oid版本(奇巧也许果冻豆虽然我不能确认现在),你应该能够打开照片应用和地方在那里找到他们。如果不是的话,那么启动文件浏览器的应用程序(例如应用程序是ASTRO文件管理器或X-PLORE)并浏览到图片/相机目录,你会看到你的图片。下一次您的媒体被重新索引(手机重启,或重新指数从别的地方触发),你应该看到在您的画廊/照片应用这些图片。如果你想以编程方式刷新你的媒体,<一个href=\"http://stackoverflow.com/questions/4144840/how-can-i-refresh-the-gallery-after-i-inserted-an-image-in-android\">here可能的帮助。最后,确保你在你的Andr​​oid清单的READ_EXTERNAL_STORAG​​E许可为指定 这个 (Android的文档)。

2)如果要保存的图像是只提供给您的应用程序,你需要将它们保存到您的应用程序的内部数据目录。看看 这个 直从Android文档。确保使用的MODE_PRIVATE标志。

3)对于这一点,你想存储你的应用程序可以访问某个文件路径。要么你可以将您的文件路径保存到一个文本文件,与其他一些文字资料,或者你可以使用SQLite数据库。最后,你可以使用像ORMLite的ORM为Android节省可能保持您的图片数据,并要坚持(标题,描述,路径等)的某些字段的Java对象。 这里 和的 这里 是如何得到的前奏开始在Android的SQLite数据库(直接从官方文档)。如果你想使用ORMLite,有大量的信息在其网站上 这里 。开发人员花费了大量的时间回答问题计算器..

您所有的问题都可以用几个简单的谷歌搜索来回答。他们是非常标准和基本的东西在Android中的事,所以你应该能够找到的资料和教程过多网上。

修改

在回答你关于第二个问题发表评论。这是我可能会做(或类似的东西):

注意,我没有测试。这是从我的头顶。如果您有更多的问题,在这里评论!

活动code ...

  mImageButton.setOnClickListener(新View.OnClickListener()
{
    公共无效的onClick(视图v)
    {
        意向意图=新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
        imageUri = CameraUtils.getOutputMediaFileUri(currentActivity,CameraUtils.MEDIA_TYPE_IMAGE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri);
        startActivityForResult(意向,REQUEST_IMAGE);
    }
}公共无效的onActivityResult(INT申请code,INT结果code,意图数据)
{
    如果(要求code == REQUEST_IMAGE)
    {
        如果(结果code == RESULT_OK)
        {
            字符串pathToInternallyStoredImage = CameraUtils.saveToInternalStorage(这一点,imageUri);
            //将路径中的位图和地方显示出来,或什么
        }
        否则,如果(结果code == RESULT_CANCELED)
        {
            //取消code
        }
    }
}

CameraUtils类code ...

 公共静态乌里getOutputMediaFileUri(整型)
{
    返回Uri.fromFile(getOutputMediaFile(类型));
}公共静态文件getOutputMediaFile(整型)
{
    文件mediaStorageDir =新的文件(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES),照相机);    createMediaStorageDir(mediaStorageDir);    返回的CreateFile(类型,mediaStorageDir);
}私有静态文件getOutputInternalMediaFile(上下文的背景下,整型)
{
    文件mediaStorageDir =新的文件(context.getFilesDir(),myInternalPicturesDir);    createMediaStorageDir(mediaStorageDir);    返回的CreateFile(类型,mediaStorageDir);
}私有静态无效createMediaStorageDir(文件mediaStorageDir)//以前是'私人无效......
{
    如果(!mediaStorageDir.exists())
    {
        mediaStorageDir.mkdirs(); //以前是'mediaStorage.mkdirs();'
    }
} //当时翻转另一条路私有静态文件的CreateFile(INT类型,文件mediaStorageDir)//以前是私人文件...
{
    字符串的timeStamp =新的SimpleDateFormat(YYYYMMDD_HHMMSS)格式(新的Date());
    文件媒体文件= NULL;
    如果(类型== MEDIA_TYPE_IMAGE)
    {
        媒体文件=新的文件(mediaStorageDir .getPath()+文件分割符+
                IMG_+ +的timeStamp.JPG);
    }
    否则,如果(类型== MEDIA_TYPE_VIDEO)
    {
        媒体文件=新的文件(mediaStorageDir .getPath()+文件分割符+
                VID_+的timeStamp +.MP4);
    }
    返回媒体文件;
}公共静态字符串saveToInternalStorage(上下文的背景下,乌里tempUri)
{
    在的InputStream = NULL;
    出的OutputStream = NULL;    文件sourceExternalImageFile =新的文件(tempUri.getPath());
    文件destinationInternalImageFile =新的文件(getOutputInternalMediaFile(上下文).getPath());    尝试
    {
        destinationInternalImageFile.createNewFile();        在=新的FileInputStream(sourceExternalImageFile);
        OUT =新的FileOutputStream(destinationInternalImageFile);        //传输的字节从到了
        字节[] buf中=新的字节[1024];
        INT LEN;
        而((LEN = in.read(BUF))大于0)
        {
            out.write(BUF,0,LEN);
        }
    }
    赶上(IOException异常E)
    {
        e.printStackTrace();
        //处理错误
    }
    最后
    {
        尝试{
            如果(在!= NULL){
                附寄();
            }
            如果(满分!= NULL){
                附寄();
            }
        }赶上(IOException异常五){
            //呃
        }
    }
    返回destinationInternalImageFile.getPath();
}

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

I can press a button, open up the native camera app, and successfully take a picture. But then when I check the Gallery or Photos native apps on my phone, the picture isn't saved there. I'm very new to Android so it's likely I'm missing something important in my code.

Questions:

1) Where are these pictures being saved?

2) Can I modify the below code somehow to save instead to internal storage, so all pictures taken with my app are private and only accessible through my app?

3) If I wanted to save these pictures to an object, along with some text/other input, what would be the best way? Should I just save a Uri or some identifier to reference the image later, or save the actual BitMap image?

Any help is greatly appreciated, thanks!

Here is my code to take the picture:

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 class taken straight from Google developer guides:

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) By looking at the code, I'd expect the pictures to be saved in a directory called 'camera' which would be found in the Pictures folder on your device (external storage). These might not instantly appear in your gallery, however in later versions of Android (Kitkat and maybe jelly-bean though I can't verify that right now) you should be able to open the Photos app and find them somewhere in there. If that is not the case, then launch a file explorer app (example apps are ASTRO File Manager or X-Plore) and browse to the Pictures/camera directory where you should see your images. The next time your media gets re-indexed (phone reboot, or a re-index triggered from elsewhere), you should see these pictures in your gallery/photo apps. If you want to refresh your media programatically, here might help. Finally, make sure you have the READ_EXTERNAL_STORAGE permission in your Android manifest as specified this (Android doc).

2) If you want to save images to be only available to your application, you need to save them to your application's internal data directory. Take a look at this straight from the Android doc. Make sure to use the MODE_PRIVATE flag.

3) For this, you would want to store the file path somewhere accessible to your app. Either you could save your file paths to a text file with some other text data, or you could use a sqlite database. Finally, you could use an ORM like ORMLite for Android to save a java object which might hold data for your picture and have some fields you want to persist (title, description, path, etc). Here and here is an intro on how to get started with SQLite database in Android (straight from the official doc). If you want to use ORMLite, there is plenty of information on their site here. The developer has spent a lot of time answering StackOverflow questions..

All of your questions can be answered with a few simple Google searches. They are very standard and basic things to do in Android, so you should be able to find a plethora of information and tutorials online.

EDIT:

In response to your comment about the second question. This is what I would probably do (or something similar):

Note that I didn't test this. It's from the top of my head. If you have more issues comment here!

Activity code...

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 class code...

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

So now you have the path pointing to your internally stored image, which you can then manipulate/load from your onActivityResult.

这篇关于相机拍照后不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆