如何使用相机拍照并在三星Galaxy S6 android上获取位图(如何在三星Galaxy S6上为相机图像创建临时文件)
我在使用三星Galaxy s6拍照时遇到了麻烦,并从中获取位图。 它可以在Galaxy Note和s5、LG G和黑莓PRIV等其他平台上正常工作。 但现在,我在三星Galaxy s6上工作时遇到了麻烦。 请告诉我谁在这方面有一些经验 我的代码在下面如何使用相机拍照并在三星Galaxy S6 android上获取位图(如何在三星Galaxy S6上为相机图像创建临时文件),android,image,camera,samsung-galaxy,samsung-galaxy-camera,Android,Image,Camera,Samsung Galaxy,Samsung Galaxy Camera,我在使用三星Galaxy s6拍照时遇到了麻烦,并从中获取位图。 它可以在Galaxy Note和s5、LG G和黑莓PRIV等其他平台上正常工作。 但现在,我在三星Galaxy s6上工作时遇到了麻烦。 请告诉我谁在这方面有一些经验 我的代码在下面 String mCurrentPhotoPath; private File createImageFile() throws IOException { // Create an image file name
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的内部存储上创建临时文件。****我知道它已经过去了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;
}
另外,它是关于API24(安卓N)和文件提供者方法的。三星S6最有可能基于Android N,因此这可能是您的情况。我认为您的问题在于棒棒糖中的URI,请查看此链接您没有看到什么?沿执行路径添加日志消息,并将日志粘贴到此处,以使此处的人清楚地看到。感谢您的回复。我想这是因为我在这段代码中使用了外部存储。但三星Galaxy S6不支持SD卡等外部存储。所以我想知道如何在三星Galaxy S6的内部存储器上创建临时文件。谢谢