我正在为我的应用程序添加一个摄像头,我基本上决定向它添加一个 Intent ,并使用内置摄像头。 Intent 有效,它称它很好,但是当它去保存时它变得很奇怪。根据 Eclipse,照片正在保存到 SD 卡中,当我进入 FileExplorer 时,我可以看到它们并将它们拉下来。然而,当我真正去探索 SD 卡时,文件并不存在。
public class C4Main extends Activity {
private static final int REQUEST_CODE = 1;
private Bitmap bitmap;
private ImageView imageView;
private Camera cam;
private SurfaceHolder sh;
private Uri fileUri;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("testing","before intent");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(1); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
Log.d("testing","fileUri: "+fileUri);
// start the image capture Intent
startActivityForResult(intent, 100);
Log.d("testing","after startactivity");
}
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 = 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 == 1){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
Log.d("testing loop","Filepath: "+mediaFile.getPath());
} else if(type == 2) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"VID_"+ timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
}
这是我正在使用的代码,是从 developer.google 复制的。正如我所说,根据 Eclipse 中的 FileExplorer,它正在保存到指定路径,但它不在我的 SD 卡上。我的硬件是运行 android 4.0.3 的 ASUS Transformer prime。 list 已设置为允许外部写入和相机使用。
如有任何帮助,我们将不胜感激。
最佳答案
我将此 Intent 用于使用内置摄像头。它会自动将图像存储在 SD 卡中。
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
flagImage=1;
startActivityForResult(cameraIntent, CAMERA_REQUEST);
关于android 相机保存,但没有显示在 sd 卡上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9747524/