【问题标题】:How to change camera save path?如何更改相机保存路径?
【发布时间】:2012-01-21 03:00:17
【问题描述】:

使用默认相机应用的照片和视频保存在 SD 卡上。

我真的需要知道是否有办法(简单或困难)更改路径以便我可以将这些文件保存在内存中

或者,如果您知道 android 市场上的另一个相机应用程序可以选择更改路径。

我不需要 SD 卡解决方案。

【问题讨论】:

    标签: java android mobile android-camera


    【解决方案1】:

    你可以这样做,

    这适用于我的情况..

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri());
    startActivityForResult(intent, TAKE_PHOTO_CODE);
    

    还有getImageUri()

    /**
     * Get the uri of the captured file
     * @return A Uri which path is the path of an image file, stored on the dcim folder
     */
    private Uri getImageUri() {
        // Store image in dcim
        // Here you can change yourinternal storage path to store those images..
        File file = new File(Environment.getExternalStorageDirectory() + "/DCIM", CAPTURE_TITLE);
        Uri imgUri = Uri.fromFile(file);
    
        return imgUri;
    }
    

    更多信息请查看How to capture an image and store it with the native Android Camera

    编辑:

    在我的代码中,我将图像存储在SDCARD,但您可以根据需要提供内部存储路径,例如/data/data/<package_name>/files/..

    您可以使用Context.getFilesDir()。但请记住,即使默认情况下它也是您的应用程序私有的,因此其他应用程序(包括媒体商店)将无法访问它。也就是说,您始终可以选择将文件设置为可读或可写。

    还有Context.getDir()Context.MODE_WORLD_WRITEABLE 可以写入其他应用程序可以写入的目录。但同样,我质疑将图像数据存储在本地存储中的必要性。用户不会意识到这一点,除非用户在使用您的应用时不会安装 SD 卡(这并不常见)。

    【讨论】:

    • 嗨,Environment.getExternalStorageDirectory() 是否返回 SD 卡上的文件?我问这个是因为作者问:更改路径,以便我可以将这些文件保存在内部存储器中。
    • @anticafe - 是的,它在 SDCARd 上返回一个文件,但只需检查我代码中的注释,我提到你可以提供内部存储路径..
    • 手机代工。来自中国。它没有SD卡的输入。这就是为什么...感谢您的代码!
    • @niki - 然后您可以将图像存储在应用程序的内部存储中。 Context.getFilesDir() 是你所需要的..
    • @user370305 因为我是 android 的新手......我把你发给我的代码放在哪里?
    【解决方案2】:

    是的,我相信可以从开发人员指南中查看此内容。

    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;
    
    /** 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 Uri getOutputMediaFile(int type){
    // To be safe, you should check that the SDCard is mounted
    // using Environment.getExternalStorageState() before doing this.
    
    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
              Environment.DIRECTORY_PICTURES), "MyCameraApp");
    // 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("MyCameraApp", "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;
    

    }

    more info here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-02-21
      • 1970-01-01
      • 2012-01-31
      相关资源
      最近更新 更多