0

我编写了一个相机活动,它可以拍摄一张照片并将其保存到内存中,我如何访问已保存照片的 Uri ?

这是我的相机活动的一些代码:

    public void onPictureTaken(byte[] data, Camera camera) {

        File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
        if (pictureFile == null){
            Log.d("Errore Doorbin", "Error creating media file, check storage permissions: ");
            return;
        }

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();
            // these lines are for the gallery to scan the SDCard manually
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES), "MyCameraApp");
                sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                    Uri.parse("file://"+ mediaStorageDir)));
           /* MediaScannerConnection.scanFile(CameraActivity.this,
                    new String[] { fos.toString() }, null,
                    new MediaScannerConnection.OnScanCompletedListener() {
                public void onScanCompleted(String path, Uri uri) {
                    // code to execute when scanning is complete
                }
           });*/
           // fos.close();
        } catch (FileNotFoundException e) {
            Log.d("Errore Doorbin", "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d("Errore Doorbin", "Error accessing file: " + e.getMessage());
        }
        catch (Exception e)
        {
            Log.d("Errore Doorbin", "errore Kolli dade!" + e.getMessage()) ;
        }
    }
};
4

1 回答 1

0

此活动获取的文件是pictureFile,因此要访问它的 Uri,您应该使用 Uri 的函数fromFile(),如下所示:

Uri photoUri = Uri.fromFile(pictureFile);

于 2013-02-19T12:40:57.370 回答