Hello. I would like to develop an application that when clicking on an image (ImageView) accesses the camera and after taking the photo updates ImageView with the image. It also has another image (ImageView1) and if you click, you access the photo library to choose a photo. After choosing the photo, update ImageView1. I was able to resolve the part of accessing the camera by clicking ImageView but I could not do the part of accessing the photo library, selecting a photo and replacing it with the current one. I think a lot of my code can be used.
My code
public class MainActivity extends ActionBarActivity {
ImageView img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = (ImageView) findViewById(R.id.imageView);
img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
abrirCamera();
}
});
}
public void abrirCamera(){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,0);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bp = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(bp);
}
public boolean onCreateOptionsMenu(Menu menu) {
// adiciona itens para a barra de ação, se ela estiver presente.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
My XML
<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" tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/imagemcamera"
android:layout_marginTop="47dp"
android:layout_below="@+id/textView2"
android:layout_alignLeft="@+id/imageView1"
android:layout_alignStart="@+id/imageView1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="59dp"
android:src="@drawable/fotos"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Clique para acessar as fotos do celular"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Clique para acessar a camera do celular"
android:id="@+id/textView2"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>