How to change camera save path?

对着背影说爱祢 提交于 2019-12-11 01:29:06

问题


Photographs and videos using the default camera app are saved on the SD card.

I really need to know if there is a way (easy or hard) to change the path so i can save those files on internal memory.

Or if you know another camera app from android market that has the option to change path.

I don't need SD card solutions.


回答1:


You can do something like,

This works in my case..

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

And 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;
}

For more info look at How to capture an image and store it with the native Android Camera

EDIT:

In my code I store images on SDCARD but you can give Internal storage path as per your need, like, /data/data/<package_name>/files/..

You can use Context.getFilesDir(). But keep in mind that even that is by default private to your app, so other applications (including the media store) will not be able to access it. That said, you always have the option to make files world read or writeable.

also Context.getDir() with Context.MODE_WORLD_WRITEABLE to write a directory that other apps can write into. But again, I question the need to store image data in local storage. Users won't appreciate that, unless users are expected to not have their SD card mounted while using your app (which is not common).




回答2:


Yes i believe it is possible check out this from the developers guide.

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



来源:https://stackoverflow.com/questions/8519688/how-to-change-camera-save-path

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!