【问题标题】:Accessing camera using ACTION_IMAGE_CAPTURE使用 ACTION_IMAGE_CAPTURE 访问相机
【发布时间】:2012-07-21 10:04:13
【问题描述】:

我要做的是创建一个新文件夹并将相机捕获的图像保存在该文件夹中。我正在尝试使用以下片段来实现它:

public class DocsAdd extends Activity
{   
    private static int TAKE_PICTURE = 1;
    private Uri outputFileUri;
    Bitmap thumbnail;
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);        
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        File filename;
        try 
        {
            long currentTime = System.currentTimeMillis();
            String fileName = "img" + currentTime+".jpg";
            String path = Environment.getExternalStorageDirectory().toString();             
            filename = new File(path + "/AutoistDiary/"+ fileName);
            FileOutputStream out = new FileOutputStream(filename);            
            out.flush();
            out.close();     
            MediaStore.Images.Media.insertImage(getContentResolver(),filename.getAbsolutePath(), filename.getName(), filename.getName());

          outputFileUri = Uri.fromFile(filename);
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        startActivityForResult(intent, TAKE_PICTURE);
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        if (requestCode == TAKE_PICTURE)
        {
            Uri imageUri = null;            
            if (data!=null) 
            {
                if (data.hasExtra("data")) 
                {
                    thumbnail = data.getParcelableExtra("data");
                    Intent docsShow_intent=new Intent(this,DocsShow.class);
                    startActivity(docsShow_intent);
                }
            } 
            else 
            {
            }

        }  
    }
}

但是当我在 Optimus1 上运行它时,它没有向我的活动返回任何结果,遇到了一个链接 Camera on Android Example,它说明了我目前面临的相同问题,因此可以帮助我解决这个问题。 ?

【问题讨论】:

  • 下面的答案对你有用吗?
  • 你为什么不试一试@JeremyWest 大部分是的,它正在工作......!

标签: android android-intent android-camera


【解决方案1】:

不幸的是,当您在相机意图中使用MediaStore.EXTRA_OUTPUT 标志时,某些设备上存在一个错误,导致onActivityResult 中的Intend data 参数为空。一种解决方法是保持outputFileUri 变量为全局变量,以便您可以在onActivityResult 方法中再次访问它。像这样的:

Uri outputFileUri; //global variable

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photo= new File(Environment.getExternalStorageDirectory(), "Autoist Diary"); 

if (! photo.exists())  
{  
    photo.mkdirs();  
    if (! photo.mkdirs())  
    {  
        Log.i("MyCameraApp", "failed to create directory");  
    }  
}  
puc_img=new File(photo,"Puc_Img.jpg");  
outputFileUri = Uri.fromFile(photo);  
intent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);  
StartActivityForResult(intent, TAKE_PICTURE);  

public void onActivityResult(int requestCode, int resultCode, Intent data)  
{  
    super.onActivityResult(requestCode, resultCode, data);  
    switch (requestCode)   
    {  
       case TAKE_PICTURE:  
           if (resultCode == Activity.RESULT_OK)   
           {                   
               if(data != null) { //if the intent data is not null, use it
                  puc_image = getImagePath(Uri.parse(data.toURI())); //update the image path accordingly
                  StoreImage(this,Uri.parse(data.toURI()),puc_img);
               }
               else { //Use the outputFileUri global variable
                  StoreImage(this,outputFileUri,puc_img);
               }
           }   
       break;
    }  
}  
public static void StoreImage(Context mContext, Uri imageLoc, File imageDir)  
{  
    Bitmap bm = null;  
    try   
    {  
        bm = Media.getBitmap(mContext.getContentResolver(), imageLoc);  
        FileOutputStream out = new FileOutputStream(imageDir);  
        bm.compress(Bitmap.CompressFormat.JPEG, 100, out);  
        bm.recycle();  
    }  
    catch (FileNotFoundException e)   
    {  
        e.printStackTrace();  
    }  
    catch (IOException e)   
    {  
       e.printStackTrace();  
    }  
    catch (Exception e)   
    {  
        e.printStackTrace();  
    }  
}
public String getImagePath(Uri uri) {
    String selectedImagePath;
    // 1:MEDIA GALLERY --- query from MediaStore.Images.Media.DATA
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    if (cursor != null) {
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        selectedImagePath = cursor.getString(column_index);
    } else {
        selectedImagePath = null;
    }

    if (selectedImagePath == null) {
        // 2:OI FILE Manager --- call method: uri.getPath()
        selectedImagePath = uri.getPath();
    }
    return selectedImagePath;
}

这个解决方法对我有用。希望对您有所帮助。

【讨论】:

  • StoreImage 出现错误(this,Uri.parse(outputFileUri),puc_img); Uri.parse() 解析一个字符串而不是 Uri.so 我应该传递给这个方法的内容
  • @sankettt 对不起我的错。我已经更新了我的答案。对于StoreImage 方法,您已经是正确的Uri
  • 代码工作正常,但我遇到了一个奇怪的问题。我提到的设备上保存的文件是 0kb。这可能是什么问题。但同时当我在其他设备上尝试时它工作正常。可能这是 2.2 的错误 .. 对此有任何帮助
  • @sankettt 我已经更新了我的答案。希望这能解决您的问题。我认为问题在于当Intent data 不为空时,onActivityResult 中的变量puc_image 没有更新。
  • 嘿@angelo 我使用了一些其他代码,这些代码在 2.3 中运行良好,但为 2.2 存储了 0kb,我将代码放在这里供您参考。!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-09
  • 2015-03-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-10-17
  • 1970-01-01
相关资源
最近更新 更多