如何在三星Galaxy S6安卓系统上用相机拍照并获取位图(如何在三星Galaxy S6上为相机拍摄的图像创建临时文件)



我在使用三星Galaxy s6拍照时遇到了麻烦,并从中获取位图。它在其他平台上正常工作,如Galaxy Note和s5、LG G@和黑莓PRIV。但现在,我在三星Galaxy s6上工作时遇到了麻烦。请给我在这方面有一些经验的解决方案。

我的代码如下。。。

    String mCurrentPhotoPath;
private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "IMG_" + timeStamp + "_";
        File albumF = getAlbumDir();
        if (albumF == null){
            return null;
        }
        File imageF = File.createTempFile(imageFileName, ".jpg", albumF);
        mCurrentPhotoPath = "file:" + imageF.getAbsolutePath();
        return imageF;
    }
    private File getAlbumDir() {
        File storageDir = null;
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            storageDir = new File(
                    Environment.getExternalStorageDirectory()
                            + "/dcim/"
                            + "test"
            );
            if (storageDir != null) {
                if (!storageDir.mkdirs()) {
                    if (!storageDir.exists()) {
                        Log.d("test", "failed to create directory");
                        CommonFunc.ShowAlertDialog(this,"test", "failed to create directory",null);
                        return null;
                    }
                }
            }
        } else {
            Log.v(getString(R.string.app_name), "External storage is not mounted READ/WRITE.");
            CommonFunc.ShowAlertDialog(this, getString(R.string.app_name), "External storage is not mounted READ/WRITE.", null);
        }
        return storageDir;
    }
    public void showUserImgCameraDialog(View v) {
        Intent takePicture = new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE);
        pFile = null;
        try {
            pFile = createImageFile();
        } catch (IOException e) {
            CommonFunc.ShowToast(this, "creatimagefile issue" + mCurrentPhotoPath);
        }
        if (pFile != null) {
            takePicture.putExtra(MediaStore.EXTRA_OUTPUT,
                    Uri.fromFile(pFile));
            try {
                startActivityForResult(takePicture,
                        100);
            } catch (Exception e) {
                CommonFunc.ShowAlertDialog(this, "test", "No Camera on Device:" + e.toString(), null);
            }
        }
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == RESULT_OK) {//upload user image from camera
            imageView = (ImageView) findViewById(R.id.ivAvatar);
            try {
                try {
                    FileInputStream in = new FileInputStream(pFile);
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 10;
                    mCurrentPhotoPath = pFile.getAbsolutePath();
                    CommonFunc.ShowToast(this, mCurrentPhotoPath);
                    Bitmap _bmp = BitmapFactory.decodeStream(in, null, options);
                    imageView.setImageBitmap(_bmp);

                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                    CommonFunc.ShowAlertDialog(this, "test", "ImageFileNotFound", null);
                }
            }catch (Exception e){}
        }
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

我看不到活动相机屏幕,我认为这是因为三星Galaxy S6上的createImageFile路径(mCurrentPhotoPath)不正确。但这个代码适用于除三星Galaxy S6以外的其他平台。

如果有人有经验,请告诉我。谢谢

ps:我想是因为我在这个代码上使用了外部存储。但三星Galaxy S6不支持像SD存储卡那样的外部存储*****所以我想知道如何在三星Galaxy S6的内部存储上创建临时文件。*****

我知道它已经过去了两年,但由于@zaltan将最初的问题改为

*****所以我想知道如何在三星Galaxy S6的内部存储上创建临时文件。*****

我想回答,其实很简单:

mInternalCacheDir = context.getCacheDir();

很可能你可以在这个目录中创建一个临时文件
或者使用以下方法:

File createImageFile() throws IOException {
    String imageFileName = "JPEG_" + System.currentTimeMillis() + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
    File imageFile = File.createTempFile(imageFileName, ".jpg", storageDir);
    return imageFile;
}

还请考虑以下问题-它是关于api 24(Android N)和FileProvider方法的。三星S6最有可能基于安卓N,因此它可能是你的情况。

相关内容

  • 没有找到相关文章

最新更新