0

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

4

1 回答 1

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

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

于 2014-11-29T06:31:36.900 回答