Ich will eine App erstellen die ein Foto machen und speichern kann. Ich weis allerdings nicht genau was ich falsch gemacht habe aber funktioniert auf jeden Fall nicht: Hier der Code
Java
import java.io.File;
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class AalBild extends Activity {
Button btn1;
ImageView iv1;
Intent bildintent;
File bildfile = new File(Environment.getExternalStorageDirectory() + "/FotoApp/bild.png");
Uri bilduri = Uri.fromFile(bildfile);
int Kameracode = 15;
Bitmap bm1;
int REQ_CODE_EXTERNAL_STORAGE_PERMISSION = 45;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aal_bild);
btn1 = (Button) findViewById(R.id.btn2);
iv1 = (ImageView) findViewById(R.id.imageView4);
if (bildfile.exists()){
bm1 = BitmapFactory.decodeFile(bildfile.getAbsolutePath());
iv1.setImageBitmap(bm1);
}
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (ActivityCompat.checkSelfPermission(AalBild.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
try {
bildintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
bildintent.putExtra(MediaStore.EXTRA_OUTPUT, bilduri);
startActivityForResult(bildintent, Kameracode);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Kamera nicht unterstützt!", Toast.LENGTH_SHORT).show();
}
}
else {
ActivityCompat.requestPermissions(AalBild.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQ_CODE_EXTERNAL_STORAGE_PERMISSION);
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK){
if(requestCode == Kameracode){
Toast.makeText(getApplicationContext(), "Bild gespeichert unter: " + bildfile.getAbsolutePath(), Toast.LENGTH_SHORT).show();
bm1 = BitmapFactory.decodeFile(bildfile.getAbsolutePath());
iv1.setImageBitmap(bm1);
}
}
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQ_CODE_EXTERNAL_STORAGE_PERMISSION && grantResults.length>0 &&grantResults[0] == PackageManager.PERMISSION_GRANTED);
try {
bildintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
bildintent.putExtra(MediaStore.EXTRA_OUTPUT, bilduri);
startActivityForResult(bildintent, Kameracode);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Kamera nicht unterstützt!", Toast.LENGTH_SHORT).show();
}
}
}
Alles anzeigen
Vielen Dank schonmal im Vorraus:D