Android 为什么我的应用程序只在安卓5或更低版本的设备上拍照?
我正在开发一个可以拍照的应用程序。我在我的安卓5手机上试过,它可以工作,但后来我试过两款安卓9和安卓7手机,当我拍照时,应用程序关闭了Android 为什么我的应用程序只在安卓5或更低版本的设备上拍照?,android,Android,我正在开发一个可以拍照的应用程序。我在我的安卓5手机上试过,它可以工作,但后来我试过两款安卓9和安卓7手机,当我拍照时,应用程序关闭了 public class MainActivity extends AppCompatActivity { final File ruta_fotos = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); final File file = new Fi
public class MainActivity extends AppCompatActivity {
final File ruta_fotos = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
final File file = new File(ruta_fotos, "DemoPicture.jpg");
private Button boton;
ImageView imagen;
public int cuenta=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imagen=(ImageView) findViewById(R.id.imagemId);
boton = (Button) findViewById(R.id.btnTomaFoto);
//Si no existe crea la carpeta donde se guardaran las fotos
file.mkdirs();
//accion para el boton
boton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String file = ruta_fotos + getCode() + ".jpg";
File mi_foto = new File( file );
try {
mi_foto.createNewFile();
} catch (IOException ex) {
Log.e("ERROR ", "Error:" + ex);
}
//
Uri uri = Uri.fromFile( mi_foto );
//Abre la camara para tomar la foto
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//Guarda imagen
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
//Retorna a la actividad
startActivityForResult(cameraIntent, 0);
}
});
}
@SuppressLint("SimpleDateFormat")
private String getCode()
{
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyymmddhhmmss");
String date = dateFormat.format(new Date() );
String photoCode = "pic_" + date;
return photoCode;
}
public void Click(View view) {
cargarImagen();
}
private void cargarImagen (){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/");
startActivityForResult(intent.createChooser(intent,"seleccione a la aplicacion"),10);
cuenta=1;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==RESULT_OK && cuenta==1){
Uri path= data.getData();
imagen.setImageURI(path);
}
}
}
我尝试更改权限,但没有成功。我留下清单来检查如何放置权限
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.tomar">
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
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 6开始,一些权限被归类为危险权限。危险的权限不仅需要在清单文件中列出,还需要运行时请求 下面是运行时请求的教程。 这是所有权限的列表,您可以通过其保护级别查看权限是否危险。
您需要调查,然后。另外,我要提到的是,通过
Intent
拍照不需要CAMERA
许可,因此您可以删除特定的
。如果您将其保留在那里,您也必须在运行时请求该权限。而且,Android 10+上的文件系统访问受到严格限制,因此如果你打算支持这些版本,你最终将不得不对此进行调整。当我尝试该应用程序时,它会标记下一个错误:2019-11-21 10:12:57.009 13889-13889/?E/libtrusty:tipc_connect:无法打开tipc设备“/dev/trusty-ipc-dev0”:没有这样的文件或目录2019-11-21 10:12:57.009 13889-13889/?E/storageproxyd:连接到存储服务器失败(-2)