【问题标题】:how to take a picture with camera and get bitmap on Samsung Galaxy S6 android (how to create temp file on Samsung galaxy s6 for the image from camera)如何用相机拍照并在三星 Galaxy S6 android 上获取位图(如何在三星 Galaxy S6 上为来自相机的图像创建临时文件)
【发布时间】:2016-04-13 12:38:19
【问题描述】:

我在使用三星 Galaxy s6 拍照时遇到问题,并从中获取位图。 它可以在 Galaxy Note 和 s5、LG G@ 和 Blackberry 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);
    }

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

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

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

【问题讨论】:

  • 我认为您的问题是棒棒糖中的 URI,请查看此链接 stackoverflow.com/questions/29646975/…
  • 你没看到什么?沿执行路径添加日志消息,并将日志粘贴到此处,以便人们清楚地看到。
  • 感谢您的回复。我认为这是因为我在这段代码上使用了外部存储。但三星 Galaxy S6 不支持 SD 存储卡等外部存储。所以我想知道如何在三星 Galaxy S6 的内部存储上创建临时文件。谢谢

标签: android image camera samsung-galaxy samsung-galaxy-camera


【解决方案1】:

我知道它已经过去了 2 年,但自从 @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;
}

还有please consider the following question - 它关于 api 24 (Android N) 和 FileProvider 方法。三星 S6 很可能基于 Android N,因此可能就是您的情况。

【讨论】:

    猜你喜欢
    • 2016-04-19
    • 1970-01-01
    • 2017-12-17
    • 2017-01-13
    • 1970-01-01
    • 1970-01-01
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多