【问题标题】:Where to store images within Internal Memory?内存中的图像存储在哪里?
【发布时间】:2015-02-05 07:17:24
【问题描述】:

我有一个自定义相机 Android 应用程序,它根据 Google 教程将捕获的图像保存到外部存储器中,然后 Media Scanner 触发 Gallery 来检测它们。

但我有一个 LG G2 的新客户端,它没有 SD 卡插槽,没有外部存储器——只有内部。

我向他解释说,我只能让我的应用程序将图像存储在应用程序的内部缓存中,他会通过某种 Root Explorer 访问它们。

但他声称其他购买的相机应用程序会存储它们的图像,以便 Gallery 可以检测到它们。如何?请帮忙——我需要让我的应用也能做到这一点。

谢谢!

UPD:以下是我的代码,适用于其他设备,主要处理外部存储器:

public static File getBaseFolder() {
    File f;
    f = null;
    try {
        f = Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    } catch (Exception e) {
        Utils.toasterLong("Error accessing storage: " + e.getMessage());
    }
    return f;
}

public static File getImageFolder() {
    File f;
    f = null;
    try {
        f = new File(getBaseFolder(), "Magic");
    } catch (Exception e) {
        Utils.toasterLong("Error accessing storage: " + e.getMessage());
    }
    return f;
}

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.

    File mediaStorageDir = getImageFolder();
    // This location works best if you want the created images to be shared
    // between applications and persist after your app has been uninstalled.

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(TAG, "failed to create directory");
            return null;
        }
    }

    // Create a media file name
    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;
}

以及将图像写入外部存储器并触发扫描仪的代码部分:

{
pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
                if (pictureFile == null) {
                    writeErrorDirty = true;
                    if (MyDebug.LOG)
                        Log.d(TAG,
                                "Error creating media file, check storage permissions");

                } else if (MyDebug.LOG)
                    Log.d(TAG, "Valid path=" + pictureFile.getAbsolutePath());

                FileOutputStream out = null;
                try {
                    out = new FileOutputStream(pictureFile);
                    picTaken_mod.compress(Bitmap.CompressFormat.JPEG, 90, out);
                } catch (Exception e) {
                    e.printStackTrace();
                    writeErrorDirty = true;
                } finally {
                    try {
                        if (out != null) {
                            out.close();
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                        writeErrorDirty = true;
                        if (MyDebug.LOG)
                            Log.d(TAG,
                                    "Error writing media file, check storage permissions");
                    }
                } // end try

try {
                MediaScannerConnection.scanFile(grandContext,
                        new String[] { pictureFile.getAbsolutePath() }, null,
                        new MediaScannerConnection.OnScanCompletedListener() {
                            public void onScanCompleted(String path, Uri uri) {
                                // now visible in gallery
                            }
                        });
            } catch (Exception e) {
                if (MyDebug.LOG)
                    Log.d(TAG,
                            "Error broadcasting the image: " + e.getMessage());
                // writeErrorDirty = true;
            }
}

【问题讨论】:

    标签: android memory camera gallery internal


    【解决方案1】:

    但我有一个 LG G2 的新客户端,它没有 SD 卡插槽,没有外部存储器——只有内部。

    您在该设备上同时拥有internal storageexternal storage。您可能没有removable storage,但可移动存储不是内部存储,也不是外部存储。

    请理解,Android SDK 所指的内部存储和外部存储与 Android SDK 相关联,因为不一定与向用户显示的信息一致(例如,在设置中)。

    但他声称其他购买的相机应用程序会存储它们的图像,以便 Gallery 可以检测到它们。怎么样?

    他们将图像写入外部存储,然后使用MediaScannerConnection 通知图库和其他使用MediaStore ContentProvider 的应用文件在那里。

    【讨论】:

    • 当您描述: -- 他们将图像写入外部存储,然后使用 MediaScannerConnection 通知 Gallery 和其他使用 MediaStore ContentProvider 的应用程序文件在那里。 - - 你也描述了我的应用程序。它适用于数百种设备,但不适用于 LG G2
    • @rommex:那么要么你没有在 G2 上写入外部存储,要么 G2 有某种错误。您可能会考虑编辑您的问题以发布您用于编写文件的代码并使其由MediaStore 索引。
    • 请在上面编辑的帖子中查看我的代码,谢谢!
    • @rommex:在out.close() 之前调用out.flush()out.getFD().sync(),以确保在使用MediaScannerConnection 之前将整个文件写入磁盘。
    • 您的意思是这可能会阻止我的应用程序在该特定设备上正常运行吗?谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 2011-08-29
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    • 2012-10-17
    相关资源
    最近更新 更多