In my code, camera should take photo and save it in ImageView. It works good until saving picture to the ImageView. It displays nothing!!
There are my errors:
And here is my code:
public class MainActivity extends AppCompatActivity {
static final int REQUESTED_IMAGE_CAPTURE=1;
ImageView output;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button shot=(Button)findViewById(R.id.button);
output=(ImageView)findViewById(R.id.imageView);
if(!hasCamera())
shot.setEnabled(false);
}
private boolean hasCamera(){
return getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY);
}
public void lanch_camera(View view){
Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,REQUESTED_IMAGE_CAPTURE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode==REQUESTED_IMAGE_CAPTURE && resultCode ==RESULT_OK){
Bundle extras=data.getExtras();
Bitmap pic=(Bitmap) extras.get("data");
output.setImageBitmap(pic);
}
}
}
Please help me. Thanks.
Related
I have a ProfileActivity:
public class ProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
final ImageView exampleImage = (ImageView) this.findViewById(R.id.exampleImageView);
exampleImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// [TODO] Implement application behavior when the user clicks the profile picture
//Toast.makeText(ProfileActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
startActivity(new Intent(ProfileActivity.this, GalleryActivity.class));
}
});
}
and I have a second activity called 'GalleryActivity'. the concept is to let the user choose a image from the gallery and then use it to replace the original Profile image.
public class GalleryActivity extends AppCompatActivity {
ImageView imageView;
private static final int PICK_IMAGE = 100;
Uri imageUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
}
however, when I click on a image of the gallery, instead of taking it as new profile image, it take me back to the original image.
what is happening and how I can solve it?
You can use Github Library it will help you.
Here is the Link.
ImagePicker.Companion.with(this)
.crop() //Crop image(Optional), Check Customization for more option
.compress(1024) //Final image size will be less than 1 MB(Optional)
.maxResultSize(1080, 1080)
.start();//Final image resolution will be less than 1080 x 1080(Optional)
On Activity Result.
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (data != null) {
selectedFileURI = data.getData();
if (selectedFileURI != null && !Objects.requireNonNull(selectedFileURI.getPath()).isEmpty()) {
btnUpdate.setVisibility(View.GONE);
civUser.setImageURI(selectedFileURI);
Glide.with(context).load(selectedFileURI).into(civUser);
btnUploadDp.setVisibility(View.VISIBLE);
} else {
Functions.showSnackBar(context, "Cannot Get this Image");
}
}
}
}
Refernce Link.
Am building a mobile app for our local football team.I want to able to pick the logo images for the two teams that will have an upcoming game.
So picking the images from the Gallery and save them.
I have created the openGalleryMethods to open Gallery and then onbtain the image that is selected at the first pick.
Then call the openGallery Method again and pick the image that is picked the second time on another imageView.
public class MatchFixturesActivity extends AppCompatActivity implements TimePickerDialog.OnTimeSetListener, DatePickerDialog.OnDateSetListener {
private EditText HostTeamInput,VisitingTeamInput,
StadiumHostingGame,LeagueOfPlay,DateOfGameDisplay;
private CircleImageView HostTeamLogo,VisitingTeamLogo;
private TextView DisplayTimeOfGame;
private Button TimeOfGameBtn,DateOfGameBtn,SavingInfoButton;
private static final int GalleryPick = 1;
private static final int GalleryPick2 = 1;
private Uri ImageUrl1,ImageUrl2;
private String HostTeamName,VisitingTeamName,StadiumHostName
,LeagueName,DateOfPlayInput,TimeInput,RandomKey,downloadImageUrl,downloadImageUrl1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_match_fixtures);
HostTeamLogo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OpenGallery();
}
});
VisitingTeamLogo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
OpenGallery1();
}
});
}
private void OpenGallery() {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,GalleryPick);
}
private void OpenGallery1() {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(galleryIntent,GalleryPick);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GalleryPick && resultCode==RESULT_OK && data!=null){
ImageUrl1 = data.getData();
HostTeamLogo.setImageURI(ImageUrl1);
}else{
ImageUrl2 = data.getData();
VisitingTeamLogo.setImageURI(ImageUrl2);
}
}
}
I expect the logo of the hosting team to be loaded into the imageview at first pick and then the second pick the logo for the visitong team to be loaded in its se[erate imageView
Guys thank and fortunately have got the answer. I had to define my GalleryPicks as
GalleryPick and GalleryPick1 and defined as
private static final int GalleryPick = 1;
private static final int GalleryPick2 = 2;
With this,every thing worked perfectly well with the same code
Why is my code not functioning? i want to transfer image from one activity to another. Please help! Note: I have create a camera function and thats where I get image.
This is the MainActivity.java
public class MainActivity extends AppCompatActivity {
public static final int CAMERA_REQUEST = 9999;
ImageView imageView1;
Button camera,send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = findViewById(R.id.imageView1);
camera = findViewById(R.id.camera);
send = findViewById(R.id.send);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent d1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(d1, CAMERA_REQUEST);
}
});
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
imageView1.buildDrawingCache();
Bitmap bitmap = imageView1.getDrawingCache();
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("bp", bitmap);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView1.setImageBitmap(bitmap);
}
}
This is the ResultActivity.java
public class ResultActivity extends AppCompatActivity {
ImageView imageView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
imageView2 = findViewById(R.id.imageView2);
Intent intent = new Intent();
Bitmap bitmap = intent.getParcelableExtra("bp");
imageView2.setImageBitmap(bitmap);
}
}
If I've done something wrong. Please let me know. Thankssss
Youre already receiving the bitmap as a Uri in the intent received in 'OnActivityResult'.
All you have to do is to use this intent to move to the second activity.
Then from there you can convert it to bitmap:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Intent intent = new Intent(MainActivity.this, ResultActivity.class);
intent.putExtra("Some key",data.getData().toString());
Bitmap bitmap = (Bitmap) data.getExtras().get("data");
imageView1.setImageBitmap(bitmap);
}
}
On ResultActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
Uri uri = Uri.parse(getIntent.getExtra.getString("Some key"));
imageView2 = findViewById(R.id.imageView2);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imageView2.setImageBitmap(bitmap);
}
}
Everything works fine without the Main2Activity but what I want to do is to play the video on Main2Activity when I click the fullscreen button. Everything works well on MainActivity but when I click the fullscreen button it crashes. Not sure why. I'm new to Android development, any help would be appreciated.
MainActivity:
public class MainActivity extends AppCompatActivity {
Button buttonPlay;
Button buttonFullScreen;
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
MediaController mediacontroller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.activity_main);
setContentView(R.layout.activity_main);
resultvideo = (VideoView)findViewById(R.id.videoView);
mediacontroller = new MediaController(MainActivity.this);
mediacontroller.setAnchorView(resultvideo);
resultvideo.setMediaController(mediacontroller);
Button click = (Button)findViewById(R.id.buttonRecord);
resultvideo = (VideoView)findViewById(R.id.videoView);
}
public void dispatchTakeVideoIntent(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
Log.i("test","111111111111111" + videoUri.toString());
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonPlay = (Button) findViewById(R.id.buttonPlay);
{
buttonPlay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
Log.i("test","111111111111111");
}
});
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
}
});
}
}
}
Main2Activity:
public class Main2Activity extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
Log.i("test", "111111111111111" + videoUri.toString());
resultvideo.setVideoURI(videoUri);
mediacontroller.setAnchorView(resultvideo);
resultvideo.pause();
}
buttonFullScreen = (Button) findViewById(R.id.buttonFullScreen);
{
buttonFullScreen.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mediacontroller.show();
mediacontroller.setAnchorView(resultvideo);
resultvideo.start();
Log.i("test","111111111111111");
}
});
}
}
}
resultvideo is declared in MainActivity, it is not available in Main2Activity
mediacontroller and resultvideo are declared in MainActivity where they are in Main2Activity? You must have them in layout for Main2Activity also, as you have them in MainActivity layout and finding them by ids in Main2Activity is also necessary.
How do I set up multiple buttons to access the camera and save the photo?
I have figured out how to have one button do this task, but cannot figure out how to get multiple buttons to do this.
My Java Code so far:
package com.example.android.phototaker;
public class MainActivity extends AppCompatActivity {
ImageView result;
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button click = (Button)findViewById(R.id.signbutton);
result = (ImageView)findViewById(R.id.imageView);
}
public void dispatchTakePictureIntent(View view) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
result.setImageBitmap(imageBitmap);
}
}
}
By multiple buttons do you mean, different buttons opening up the camera?
If so, you will need to add an onclick listener for those buttons.