Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – How to take a picture and replace the image in android

Posted on November 11, 2022 By

Solution 1 :

first off all you have to use camera permission in you manifest

<uses-permission android_name="android.permission.CAMERA" />
<uses-feature android_name="android.hardware.camera"
              android_required="true" />

then in your activity or fragment use camera intent to load camera api

int REQUEST_IMAGE_CAPTURE=2001 

 Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }

then in your onActivityResult handle data and set to your image view

@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");
        imageView.setImageBitmap(imageBitmap);
    }
}

you can find more information here https://developer.android.com/training/camera/photobasics
i hope was helpfull

Solution 2 :

to open camera

 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
 startActivityForResult(cameraIntent, PICK_FROM_CAMERA);

add permission

<uses-permission android_name="android.permission.CAMERA" />

and take the data from onActivityResult and set it to imageView

Problem :

I have a floating action button (FAB), when I press the FAB it will appear the dialog layout. how can I take a picture then replace the ImageView with picture that i take when the button text name “Shoot” clicked ?

here my activity_store.xml for FAB :

<RelativeLayout
        android_layout_width="match_parent"
        android_layout_height="91dp"
        android_layout_below="@+id/listviewLayout">

        <com.google.android.material.floatingactionbutton.FloatingActionButton
            android_id="@+id/fabAdd"
            android_layout_width="77dp"
            android_layout_height="91dp"
            android_layout_alignParentRight="true"
            android_backgroundTint="@color/white"
            android_src="@drawable/ic_plus"
            app_elevation="6dp"
            app_fabSize="normal"
            android_layout_marginRight="20dp"
            android_onClick="addStock"/>
    </RelativeLayout>

here my dialog_add_storestock.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns_android="http://schemas.android.com/apk/res/android"
    xmlns_app="http://schemas.android.com/apk/res-auto"
    android_orientation="vertical"
    android_layout_width="match_parent"
    android_layout_height="match_parent">

    <LinearLayout
        android_id="@+id/addstockLayout"
        android_layout_width="match_parent"
        android_layout_height="wrap_content"
        android_orientation="vertical"
        android_layout_marginLeft="10dp"
        android_layout_marginRight="10dp"
        android_layout_marginBottom="20dp"
        android_layout_marginTop="10dp">

        <androidx.appcompat.widget.AppCompatButton
            android_id="@+id/buttonTakePicture"
            android_layout_width="match_parent"
            android_layout_height="wrap_content"
            android_text="Shoot"
            android_onClick="TakePicture"
            />

        <ImageView
            android_id="@+id/resultImage"
            android_layout_width="match_parent"
            android_layout_height="wrap_content"
            android_src="@drawable/ic_launcher_background"
            />

    </LinearLayout>
</RelativeLayout>

here my StoreActivity.java :

public class StoreActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_store);

}


public void addStock(View view){
    LayoutInflater inflater = this.getLayoutInflater();
    View alertLayout = inflater.inflate(R.layout.dialog_add_storestock, null);

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
    alert.setTitle("Add Data");
    alert.setView(alertLayout);
    alert.setCancelable(false);

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            //
        }
    });

    alert.setPositiveButton("Add", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Toast.makeText(getBaseContext(), "Data Added!", Toast.LENGTH_SHORT).show();
        }
    });

    AlertDialog dialog = alert.create();
    dialog.show();
}

Sorry for bad english.

READ  [FIXED] java - Mapbox: Do we need LineLayer if we want to display a Line on the map
Powered by Inline Related Posts

Comments

Comment posted by pohon gems

where i can put the Open Camera code? inside public void addStock ?

Comment posted by Muhamed El-Banna

@pohongems if you want to open it from alertDialog .. so put openCamera in Positive button of alertDialog

Android Tags:android, java

Post navigation

Previous Post: [FIXED] android – How to pass data by Back Button, Kotlin
Next Post: [FIXED] Project with external module issue in android studio 3.6

Related Posts

[FIXED] android – Coroutines, async DiffUtil and Inconsistency detected error Android
[FIXED] android – How to show an incoming call when screen is locked on react-native Android
[FIXED] java – How to deal with OutOfMemoryError in Android Android
[FIXED] firebase – Can I use single input form to collect data from multiple Activities in Android Studio? Android
[FIXED] java – Shadow is not working on the textview. How to fix it? Android
[FIXED] android – Convert from kotlin to c# Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme