STEP 1:Create activity_pic.xml in layout res/layout folder
activity_pic.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" /> <Button android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/intent" /> <ImageView android:layout_width="150dp" android:layout_height="150dp" android:id="@+id/intentimage" android:layout_below="@+id/intent" android:background="@drawable/about" /><!--select any image from drawable folder--> </RelativeLayout> |
Step 2 : add permission in AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.picactivitypro"> <uses-feature android:name="android.hardware.camera" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> </manifest> |
Step 3 :Create file Pictureselect.java in main/java folder
Pictureselect.java
public class Pictureselect.java extends AppCompatActivity{
Button intentes; ImageView images; final int TAKE_PICTURE=1; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_pic); images=(ImageView)findViewById(R.id.intentimage); intentes=(Button)findViewById(R.id.intent); intentes.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { selectImage(); } }); } } private void selectImage(){ final CharSequence[]options={"Take pic","Choose pic from Gallery","Cancel"}; AlertDialog.Builder builder=new AlertDialog.Builder(trail.this); builder.setTitle("Add Photo!"); builder.setItems(options,new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog,int item){ if(options[item].equals("Take Photo")) { Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent,TAKE_PICTURE); } else if(options[item].equals("Choose from Gallery")) { Intent intent=new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); startActivityForResult(intent,2); } else if(options[item].equals("Cancel")){ dialog.dismiss(); } } }); builder.show(); } @Override protected void onActivityResult(int requestCode,int resultCode,Intent data){ super.onActivityResult(requestCode,resultCode,data); if(resultCode==RESULT_OK){ if(requestCode==1){ Bitmap photo=(Bitmap)data.getExtras().get("data"); images.setImageBitmap(photo); } } else if(requestCode==2) { Uri selectedImage=data.getData(); String[]filePath={MediaStore.Images.Media.DATA}; Cursor c=getContentResolver().query(selectedImage,filePath,null,null,null); c.moveToFirst(); int columnIndex=c.getColumnIndex(filePath[0]); String picturePath=c.getString(columnIndex); c.close(); Bitmap thumbnail=(BitmapFactory.decodeFile(picturePath)); images.setImageBitmap(thumbnail); } } } |
No comments:
Post a Comment