将文件保存到内部存储中并使文件可供用户访问

时间:2014-11-29 06:26:51

标签: android camera internal-storage

我正在制作相机应用,我想将拍摄的照片保存到内部存储中。我希望用户可以访问所有照片,而不仅仅是我的应用。我想我不应该通过android使用File类。有什么建议吗?

1 个答案:

答案 0 :(得分:0)

在你的onClick调用capturePicture():并复制代码下面的粘贴

private void capturePicture()
    {
        if (isCameraPresent())
        {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            fileUri = Uri.fromFile(getOutputMediaFile());
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(intent, Global.CAMERA_CAPTURE_REQUEST_CODE);
        }
    }

    private File getOutputMediaFile()
    {
        // External sdcard location
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                Global.IMAGE_DIRECTORY_NAME);
        // Global.IMAGE_DIRECTORY_NAME is String in Global value is abc/images
        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists())
        {
            if (!mediaStorageDir.mkdirs())
            {
                Log.d(Global.IMAGE_DIRECTORY_NAME, "Oops! Failed create " + Global.IMAGE_DIRECTORY_NAME + " directory");
                return null;
            }
        }
        File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "CAPTURE" + ".jpg");


        return mediaFile;
    }

    private boolean isCameraPresent()
    {
        if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA))
            return true;// this device has a camera
        else
            return false;// no camera on this device
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == Global.CAMERA_CAPTURE_REQUEST_CODE)
        {
            if (resultCode == RESULT_OK)
            {
                // Image is saved into internal pictures folder >> abc folder>>Images
            }
            else
            {
                // failed to capture image
                Toast.makeText(getApplicationContext(), "Sorry! Failed to capture image", Toast.LENGTH_SHORT).show();
            }
        }
    }

这将在内部存储/图片/ abc / images

中保存捕获的图像

希望这是你想要的,这有助于你:)