Friday, 19 October 2018

Android Take Image From Phone And Add In Imageview

1) Manifext.xml

 Add This Permission On Your Manifest.mls File

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>


2) Mainactivity.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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            tools:srcCompat="@tools:sample/avatars[0]"
            android:visibility="gone"/>



        <Button
            android:id="@+id/btn_take"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Get Picture"/>


        <Button
            android:id="@+id/btn_upload"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Upload Picture"
            android:layout_marginTop="10dp"/>
    </LinearLayout>

</RelativeLayout>

3) Mainactivity.java

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private Button btn_take;
    //Request code for taking image
    private final int IMG_REQUEST = 1;
    //Bitmap for geting image and set into imageview
    private Bitmap bitmap;

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

        imageView = (ImageView) findViewById(R.id.imageView);
        btn_take = (Button) findViewById(R.id.btn_take);

        btn_take.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectImg();
            }
        });

    }

    //Function for open gallry to select image
    private void selectImg(){
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(intent, IMG_REQUEST);  // add your image code here
    }

    //For adding onActivityResult type alt + insert
     //Find onActivityResult and added

     @Override
     protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
         super.onActivityResult(requestCode, resultCode, data);

         //my conditions to set a image into imageview

         if(requestCode == IMG_REQUEST && resultCode == RESULT_OK && data!=null) {
             //To add a get a image file

             Uri path = data.getData();

             try {
                 //Assign image into bitmap
                 bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), path);
                 imageView.setImageBitmap(bitmap);
                 imageView.setVisibility(View.VISIBLE);
             } catch (IOException e) {
                 e.printStackTrace();
             }
         }
     }
 }

No comments:

Android Studio - Get Current Latitude And Longitude

1. Add this dependencies in your gradle file     implementation 'com.google.android.gms:play-services-location:18.0.0' 2. Add this p...