2014-09-22 72 views
0

我在三星注测试我的应用程序, 这里是我如何用来调用相机相机活动返回null URI

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 
     } 
    } 
} 

现在我的代码工作很好,但只有当我写外部存储..内部存储始终不会从相机活动返回。

我的问题是:

  1. 为什么不能我写的内部存储?
  2. 如何将“未隐藏”的图像文件写入外部存储?
  3. 为什么旧代码停止工作?
+0

“为什么我不能写入内部存储?” - 因为相机应用无法访问应用的内部存储空间。 – CommonsWare 2014-09-22 14:45:49

+0

是的。当你传递一个'EXTRA_OUTPUT'时,三星相机应用会从android 4.3返回null。但你真的不需要'data.getData()'Uri,因为你已经知道Uri是什么 – njzk2 2014-09-22 14:48:10

+0

谢谢@CommonsWare – sunil 2014-09-22 14:57:46

回答

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

相关问题