使用相机拍照,保存到本地——而不是保存到SD卡

发布于 2024-12-01 09:53:01 字数 239 浏览 1 评论 0原文

正如标题所述,我正在尝试启动相机并拍摄一张可以在本地与应用程序一起使用的照片。这是针对 Motorola Xoom 的,默认情况下,它们不附带 SD 卡。或者至少我的客户正在考虑购买的产品没有 SD 卡。

我找到了一个关于如何启动相机的非常好的教程,我可以让它启动以及物理保存图像然后能够重新使用该图像。

我发现的许多解决方案或其他教程仅展示如何保存到 SD 卡,而不是常驻内存或应用程序本地。看起来可以这样做,但我很困惑。 :-/

As the title states, I'm trying to launch the camera and take a picture that I can use with the APP locally. This is for the Motorola Xoom and, by default, they do not come with SD cards. Or at least the ones my client is looking into purchasing do not have SD cards.

I've found a pretty nice tutorial on how to launch the camera and I can get it to launch and everything up to it physically saving the image and then being able to re-use that image.

A lot of the solutions or other tutorials I've found only show how to save to an SD card -- not to resident memory or local to the app. It looks like it is possible to do so but am stumped. :-/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

那小子欠揍 2024-12-08 09:53:01

您也许可以将其加载到 ContentProvider,以便稍后可以将图像用于任何用途:

// Add some parameters to the image that will be stored in the Image ContentProvider
int UNIQUE_BUCKET_ID = 1337;
ContentValues values = new ContentValues(7);
values.put(MediaStore.Images.Media.DISPLAY_NAME,"name of the picture");
values.put(MediaStore.Images.Media.TITLE,"Thats the title of the image");
values.put(MediaStore.Images.Media.DESCRIPTION, "Some description");
values.put(MediaStore.Images.Media.BUCKET_DISPLAY_NAME,"Album name"); 
values.put(MediaStore.Images.Media.BUCKET_ID,UNIQUE_BUCKET_ID);
values.put(MediaStore.Images.Media.DATE_TAKEN,System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

// Inserting the image meta data inside the content provider
Uri uri = getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);

// Filling the real data returned by the picture callback function into the content provider
try {
    OutputStream outStream = getContentResolver().openOutputStream(uri);
    outStream.write(buffer);  // buffer is the data stream returned by the picture callback
    outStream.close();
}catch (Exception e) {
    Log.e(TAG, "Exception while writing image", e);
}

You could perhaps load it to ContentProvider so that you could use the images later for anything:

// Add some parameters to the image that will be stored in the Image ContentProvider
int UNIQUE_BUCKET_ID = 1337;
ContentValues values = new ContentValues(7);
values.put(MediaStore.Images.Media.DISPLAY_NAME,"name of the picture");
values.put(MediaStore.Images.Media.TITLE,"Thats the title of the image");
values.put(MediaStore.Images.Media.DESCRIPTION, "Some description");
values.put(MediaStore.Images.Media.BUCKET_DISPLAY_NAME,"Album name"); 
values.put(MediaStore.Images.Media.BUCKET_ID,UNIQUE_BUCKET_ID);
values.put(MediaStore.Images.Media.DATE_TAKEN,System.currentTimeMillis());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");

// Inserting the image meta data inside the content provider
Uri uri = getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);

// Filling the real data returned by the picture callback function into the content provider
try {
    OutputStream outStream = getContentResolver().openOutputStream(uri);
    outStream.write(buffer);  // buffer is the data stream returned by the picture callback
    outStream.close();
}catch (Exception e) {
    Log.e(TAG, "Exception while writing image", e);
}
殤城〤 2024-12-08 09:53:01

查看 Activity.getFilesDir() 和 < a href="http://developer.android.com/reference/android/content/ContextWrapper.html#getCacheDir%28%29" rel="nofollow">Activity.getCacheDir() 它们提供对存储应用程序的存储的访问。另请查看 Environment.getExternalStoragePublicDirectory ()< /a>,外部,这里指的是应用程序的私有目录之外。还有其他几个方法,因此请查看 API 的类似命名方法以找到最适合您的方法。

Check out Activity.getFilesDir() and Activity.getCacheDir() they provide access to storage where the app is stored. Also check out Environment.getExternalStoragePublicDirectory (), external, here, means outside the application's private directories. There are several others so look around the API's similarly named methods to find the best fit for you.

自此以后,行同陌路 2024-12-08 09:53:01

因此,您应该注意的一件事是,当您调用Environment.getExternalStorageDirectory()时,这不一定对应于外部存储空间(即SD卡),并且会返回设备上的本地存储空间,而无需SD 卡插槽。

注意:不要对这里的“外部”一词感到困惑。该目录可以更好地被视为媒体/共享存储。它是一个可以保存相对大量数据并且在所有应用程序之间共享的文件系统(不强制执行权限)。传统上,这是一张 SD 卡,但它也可以实现为设备中的内置存储,与受保护的内部存储不同,并且可以作为文件系统安装在计算机上。

http://developer.android.com/reference/android/os/Environment .html#getExternalStorageDirectory()

同样适用Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

So one thing you should note is that when you call Environment.getExternalStorageDirectory(), this does not necessarily correspond to an external memory space (i.e. and SD card) and would return a local memory space on devices without SD card slots.

Note: don't be confused by the word "external" here. This directory can better be thought as media/shared storage. It is a filesystem that can hold a relatively large amount of data and that is shared across all applications (does not enforce permissions). Traditionally this is an SD card, but it may also be implemented as built-in storage in a device that is distinct from the protected internal storage and can be mounted as a filesystem on a computer.

http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()

The same applies for Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文