2016-03-02 66 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

相關問題