android 相机保存,但没有显示在 sd 卡上

标签 android android-camera sd-card

我正在为我的应用程序添加一个摄像头,我基本上决定向它添加一个 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/

相关文章:

java - 如何在 android 中再次打开 arraylist 时显示预选复选框

android - Android 中的 BLE 广告

android - 改造不解析数据

android - 获取android相机设备的视角

android - 打开 Galaxy Nexus 手电筒

android - 如果我的应用程序安装在 SD 卡上,私有(private)数据是否也在那里?

android - 如何从 Android 应用程序安装 .cap 小程序?

java - 错误 :Execution failed for task ':app:dexDebug'

java - 如何使用Camera2删除图像中的GPS数据?

android - 如何从SD卡中的文件中选择文件?