【问题标题】:Use Camera to take a pic, save it locally -- not to SD Card使用相机拍照,将其保存在本地 - 而不是 SD 卡
【发布时间】:2011-08-24 21:35:49
【问题描述】:

正如标题所述,我正在尝试启动相机并拍摄可以在本地与 APP 一起使用的照片。这适用于摩托罗拉 Xoom,默认情况下,它们不附带 SD 卡。或者至少我的客户正在考虑购买的那些没有 SD 卡。

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

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

【问题讨论】:

    标签: android camera local-storage xoom


    【解决方案1】:

    您也许可以将其加载到 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);
    }
    

    【讨论】:

    • 不会使用它仍然将图像存储在 SDCARD 上吗?如果您使用 .insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI),它还能工作吗?
    • 哎呀,我本来打算这样做的。
    【解决方案2】:

    查看Activity.getFilesDir()Activity.getCacheDir(),它们提供对存储应用程序的存储的访问权限。还可以查看Environment.getExternalStoragePublicDirectory (),这里的外部是指在应用程序的私有目录之外。还有其他几个,因此请查看 API 的类似名称的方法,以找到最适合您的方法。

    【讨论】:

      【解决方案3】:

      所以你应该注意的一点是,当你调用Environment.getExternalStorageDirectory()时,这不一定对应一个外部内存空间(即和SD卡),并且会在没有SD卡插槽的设备上返回一个本地内存空间。

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

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-08
        • 2012-05-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多