Android sdk,除了将图像保存在SD卡中之外,还有什么地方可以保存照片?

发布于 2024-10-16 10:11:21 字数 128 浏览 4 评论 0原文

我从网上阅读了很多关于 Android 图像存储的参考资料。他们中的大多数人都说图像存储在 SD 卡内,与用相机拍摄的照片相同。

android 是否允许我们将它们存储在 android 手机的本地内存中而不是 SD 卡中?

I have read alot of references from the web about android storage for images. Most of them were saying Images are stored insided sd card same as photos that was taken with camera.

Does android allow us to store them in local memory in the android phone rather than sd cards?

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

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

发布评论

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

评论(4

混吃等死 2024-10-23 10:11:21

要在内部存储中打开/创建文件,请使用 openFileOutput(String name, int mode)

http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int)

To open/create a file in the internal storage, use openFileOutput(String name, int mode)

http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int)

遥远的她 2024-10-23 10:11:21

您不想将图片保存到本地内存中。有些用户只有几 MB 的可用应用程序空间 - 这会被 1-2 张图片耗尽。我知道我的旧 G1 在空间不足时不会接听电话、短信或任何通知 - 它会变成镇纸。也许以后的版本会更好。

但答案是肯定的。您可以将图像保存到本地的 sqlite 数据库 - 而不是 SD 卡上。

You don't want to save pictures to your local memory. Some users only have a few MB of free application space - which would be killed by 1-2 pictures. I know my old G1 wouldn't accept call, texts, or any notifications when it was out of space - it becomes a paperweight. Maybe later versions are better.

The answer though, is yes. You can save the image to sqlite database which is local - not on SD card.

凹づ凸ル 2024-10-23 10:11:21

我使用 getCacheDir(),如 此处 中“保存”下所述缓存文件。

I use getCacheDir() as described in HERE under Saving cache files.

錯遇了你 2024-10-23 10:11:21

是的,您可以将图像存储在缓存存储中,它将是私有的,除了您的应用程序包之外,没有人可以看到该图像。

public static String saveToCacheMemory(Activity activity, Bitmap 

bitmapImage){

    SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);

    ContextWrapper cw = new ContextWrapper(activity);
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,mDateFormat.format(new Date())  + StaticVariables.imageFormat);

    Log.d(TAG, "directory: " + directory);

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        Log.d(TAG, "bit exception: Success" );
    } catch (Exception e) {
        Log.d(TAG, "bit exception: " + e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "io exce: " + e.getMessage());
        }
    }
    Log.d(TAG, "absolute path " + directory.getAbsolutePath());
    return mypath.getAbsolutePath();
}

如果您有更多问题您可以查看我的博客帖子

yeah you can store the images in cache storage , it will be private no one can see the image except from your application package.

public static String saveToCacheMemory(Activity activity, Bitmap 

bitmapImage){

    SimpleDateFormat mDateFormat = new SimpleDateFormat("yyyyMMddHHmmss", Locale.US);

    ContextWrapper cw = new ContextWrapper(activity);
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory,mDateFormat.format(new Date())  + StaticVariables.imageFormat);

    Log.d(TAG, "directory: " + directory);

    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(mypath);
        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        Log.d(TAG, "bit exception: Success" );
    } catch (Exception e) {
        Log.d(TAG, "bit exception: " + e.getMessage());
        e.printStackTrace();
    } finally {
        try {
            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
            Log.d(TAG, "io exce: " + e.getMessage());
        }
    }
    Log.d(TAG, "absolute path " + directory.getAbsolutePath());
    return mypath.getAbsolutePath();
}

if you have more questions you can check my blog post

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