-3
當我構建項目時,我選擇了android 4.0.3 IceCreamSandwich。之後,我安裝在android os.6和android os.4手機上。我意識到只適用於操作系統4,較新的API不能支持較舊的API?這聽起來不是邏輯.........Android OS 6 Marshallow無法在相機上運行4.0.3版應用程序?
Andoird manifast
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.jackd.testflash_26_4_2017">
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Android的主要
package com.example.jackd.testflash_26_4_2017;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Camera camera;
private Camera.Parameters params;
Button btnFlash;
TextView txtArea,txtArea2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnFlash = (Button) findViewById(R.id.btnFlash);
final String myString = "110101010101";
final long blinkDelay = 50;
btnFlash.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
for(int i = 0;i<myString.length();i++){
if(myString.charAt(i) == '0'){
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
}
else{
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
}
try{
Thread.sleep(blinkDelay);
}catch(InterruptedException e){
e.printStackTrace();
}
}
}
});
if (isFlashSupported()) {
camera = Camera.open();
params = camera.getParameters();
} else {
showNoFlashAlert();
}
}
private void showNoFlashAlert() {
new AlertDialog.Builder(this)
.setMessage("Your device hardware does not support flashlight!")
.setIcon(android.R.drawable.ic_dialog_alert).setTitle("Error")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
finish();
}
}).show();
}
private boolean isFlashSupported() {
PackageManager pm = getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
}
@Override
protected void onDestroy() {
if(camera != null){
camera.stopPreview();
camera.release();
camera = null;
}
super.onDestroy();
}
}
的Android XML佈局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jackd.testflash_26_4_2017.MainActivity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btnFlash"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="124dp" />
</RelativeLayout>
PS :我打開了所有的許可證攝像頭硬件。在OS6中,它需要手動打開,我已經完成了!
回到問題,Version6 Marshmallow android不支持version4 iceCreamSandwith?但是,邏輯上更高的版本應該支持更低的版本....感謝您的幫助!