2016-03-02 59 views
1

Android 6.01给出“不幸的是,相机已停止”错误,如果我们旋转我们的手机,而视频capture.this只发生在Android 6.01 update.all其他Android视觉捕获开始后锁定相机。但是如果我们旋转手机,android 6.01相机会旋转。这在默认相机中不会发生。仅当我们使用Intent捕捉视频时才会发生这种情况。不幸的是,相机已停止错误在Android 6.01

activity_video_app_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" 
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".VideoAppMainActivity"> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Record" 
    android:id="@+id/recordButton" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentStart="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Play" 
    android:id="@+id/playButton" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentEnd="true" 
    android:singleLine="true" /> 

<VideoView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/videoView" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_above="@+id/recordButton" /> 

VideoAppMainActivity

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.VideoView; 


public class VideoAppMainActivity extends Activity { 

    private Button mRecordView, mPlayView; 
    private VideoView mVideoView; 
    private int ACTIVITY_START_CAMERA_APP = 0; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_video_app_main); 

     mRecordView = (Button) findViewById(R.id.recordButton); 
     mPlayView = (Button) findViewById(R.id.playButton); 
     mVideoView = (VideoView) findViewById(R.id.videoView); 

     mRecordView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent callVideoAppIntent = new Intent(); 
       callVideoAppIntent.setAction(MediaStore.ACTION_VIDEO_CAPTURE); 

       startActivityForResult(callVideoAppIntent, ACTIVITY_START_CAMERA_APP); 
      } 
     }); 

     mPlayView.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mVideoView.start(); 
      } 
     }); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if(requestCode == ACTIVITY_START_CAMERA_APP && resultCode == RESULT_OK) { 
      Uri videoUri = data.getData(); 
      mVideoView.setVideoURI(videoUri); 
     } 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_video_app_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 
} 

menu_video_app_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" tools:context=".VideoAppMainActivity"> 
    <item android:id="@+id/action_settings" android:title="@string/action_settings" 
     android:orderInCategory="100" android:showAsAction="never" /> 
</menu> 

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="nigelhenshaw.com.videoapp" > 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".VideoAppMainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

的strings.xml

<resources> 
    <string name="app_name">VideoApp</string> 

    <string name="hello_world">Hello world!</string> 
    <string name="action_settings">Settings</string> 
</resources> 
+0

尝试到您的应用程序的权限 可能ü没有权限访问 –

+0

“如果我们在视频捕捉时旋转手机,“Android 6.01给出了”不幸的是,相机已停止“错误 - 除非您的应用程序被命名为”相机“,否则无论相机应用程序是否处理您的请求,都会发现错误。对此你几乎没有办法。 – CommonsWare

+0

这不是权限问题 – heshjse

回答

-1

从6.0棉花糖你必须采取许可 有android的 检查两种类型的许可此链接

Go here and read and try to get permission dynamically when user open camera 1st time

或者你可以试试这个代码

private void requestPermission() 
    { 
     checkPermission(); 

     if (ActivityCompat.shouldShowRequestPermissionRationale(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE)){ 

      Toast.makeText(getApplicationContext(),"Storage permission allows us to send Image. Please allow in App Settings for Complete your order.",Toast.LENGTH_LONG).show(); 

     } else { 

      ActivityCompat.requestPermissions(activity,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},PERMISSION_REQUEST_CODE); 
     } 
    } 

    private boolean checkPermission(){ 
     int result = ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE); 
     if (result == PackageManager.PERMISSION_GRANTED){ 

      return true; 

     } else { 

      return false; 

     } 
    } 

添加这对您的点击

// click of Button ImageCamera 
case R.id.ImageCamera: 
       if(!checkPermission()) 
       requestPermission(); 
       break; 

此外,在清单中添加这个

<!--external storage --> 
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
+1

这不是我们在捕获时旋转时发生的权限问题。 – heshjse