我在三星Note上测试我的应用, 这是我以前用来调用相机的方式
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, CAMERA_CAPTURE);
然后在活动结果中:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if(requestCode == CAMERA_CAPTURE)
{
picUri = data.getData();
//continue my code
}
}
}
但最近我将测试更改为Galaxy Nexus,我注意到data.getData()现在返回null而不是临时图像uri。 我读了几条建议,并且都提到将文件传递给相机活动作为新iamge的位置。 现在我将代码更改为:
....
File photoFile = createImageFile();
//photoFile = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(captureIntent, CAMERA_CAPTURE);
....
使用方法
private File createImageFile() throws IOException
{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
//File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "alboom");
//File storageDir = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES), "alboom");
//File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
//File storageDir = getFilesDir(); //not working
//File storageDir = getDir("mydir", Context.MODE_PRIVATE); //not working
//File storageDir = getCacheDir(); //not working
if (!storageDir.mkdirs()) //required if creating a new dir
Log.e(TAG, "Directory not created");
//File image = File.createTempFile( imageFileName, ".jpg", storageDir );
File image = new File(storageDir, imageFileName);
//File image = File.createTempFile(imageFileName, ".jpg"); //not working
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
和结果处理
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if(requestCode == CAMERA_CAPTURE)
{
picUri = Uri.fromFile(photoFile);
//continue my code
}
}
}
现在我的代码运行良好,但只有当我写入外部存储时..内部存储总是不会从相机活动返回。
我的问题是:
答案 0 :(得分:0)
有时候,尽管写了这样的话:
File source = null;
source = new File(imageUri.getPath());
或:
Uri url= Uri.parse(stringPath);
...你可能为你的uri获得一个空值。它似乎与最后一个Android版本(4.4.4)的错误有关。怎么办呢?一个技巧是强制保存新图像以刷新路径:
Uri tempUri = getImageUri(getApplicationContext(), imageBitmap);
(...)
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}
试试吧!您还可以take a look at this。