2014-02-12 70 views
0

在我的应用程序中,有一个按钮启动相机意图并将捕获的图像保存在给定路径中。当用户点击摄像头的右标记或保存选项时,捕获的图像被保存。与此同时,当用户点击右标记或从相机保存选项时,Camera intent启动器活动的onActivityResult()被调用。它一切正常,但在某些设备上,相机意图通过点击按钮启动,但当用户在捕获图像后单击保存按钮时,相机将不会关闭,也不会返回到onActivityResult()。为了启动相机,我正在使用这个意图。安卓相机意图不能在某些设备上工作

String path = Environment.getExternalStorageDirectory().toString(); 
Log.d("PATH", path); 
File myNewFolder = new File(path + "/it/Snapshots/"); 
myNewFolder.mkdirs(); 
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path + "/it/Snapshots/"+ic+".jpg"))); 
startActivityForResult(cameraIntent,1888); 

请帮忙解决这个问题......我很欣赏你的宝贵answers.Thanks提前

+1

你确定/ it/snaphots是可写的(或存在)吗? – Rob

+0

@Rob是不确定可写,但它会创建如果不存在,那么通过.mkdirs()以任何方式在某些设备上工作的谨慎 –

+1

您可能没有任何外部存储? – StuStirling

回答

1

使用此代码:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 

mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg")); 

    intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri); 

    try { 
     intent.putExtra("return-data", true); 
     startActivityForResult(intent, PICK_FROM_CAMERA); 
    } 
    catch (ActivityNotFoundException e) 
    { 
     e.printStackTrace(); 
    } 

在您的onActivityResult:

if (intent != null && resultcode == RESULT_OK) 
      {    

        Uri selectedImage = intent.getData(); 

        String[] filePathColumn = {MediaStore.Images.Media.DATA}; 
        Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null); 
        cursor.moveToFirst(); 
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
        String filePath = cursor.getString(columnIndex); 
        Log.v("log","filePath is : "+filePath); 

        cursor.close(); 
        try 
        { 
         ExifInterface exif = new ExifInterface(filePath); 
         orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
         //Toast.makeText(getApplicationContext(), ""+orientation, 1).show(); 
         Log.v("log", "ort is "+orientation); 

        } 
        catch (IOException e) 
        { 
         e.printStackTrace(); 
        } 

        if(bmp != null && !bmp.isRecycled()) 
        { 
         bmp = null;    
        } 

        File f = new File(filePath); 

        if (orientation==6) 
        { 
         Matrix matrix = new Matrix(); 
         matrix.postRotate(90); 
         bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 
        } 
        else if (orientation==8) 
        { 
         Matrix matrix = new Matrix(); 
         matrix.postRotate(270); 
         bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 
        } 

        else if (orientation==3) 
        { 
         Matrix matrix = new Matrix(); 
         matrix.postRotate(180); 
         bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 
        } 

        else if (orientation==0) 
        { 
         Matrix matrix = new Matrix(); 
         matrix.postRotate(0); 
         bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true); 
        } 



      } 
      else 
      { 
       Log.v("log", "Photopicker canceled");   
      } 
+0

感谢您的宝贵答案...我发现外部存储问题..没有外部存储器,为什么它不工作...你能帮我吗如何创建内部目录记忆 –

相关问题