I’m a beginner. I am writing one Android studio (Koala) application a simple messenger, and I need to be able to select an image from the gallery and open it in my application and open camera for make a photo (example to send it). Thank`s
i`ve tried search how open camera, but when i write code and debug app, i get mistake
package com.task;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Objects;
public class MainActivity extends AppCompatActivity {
private final ImageView image = findViewById(R.id.cameraImage);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
@SuppressLint("WrongViewCast") Button cam = findViewById(R.id.camera);
cam.setOnClickListener(this::onClick);
}
public void onClickHistory(View view){
startActivity(new Intent(MainActivity.this, ActivityHistory.class));
}
public void onClickExplorer(View view){
// TODO:
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = null;
if (data != null) {
bitmap = (Bitmap) Objects.requireNonNull(data.getExtras()).get("data");
}
image.setImageBitmap(bitmap);
}
private void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
}
}
New contributor
Михаил Коваль is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.