Access photo library via the android application

1

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>
    
asked by anonymous 28.12.2014 / 03:54

1 answer

1

Luiz, good evening! I was able to upload the images from my library, but it only worked for the images of what's up (maybe the size of the images.) Anyway, follow the code:

First in AndroidManifest.xml, before the application tag:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Now in the main class (MainActivity):

private Bitmap bitmap;

public void carregarGaleria(){
   Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
   intent.setType("image/*");
   intent.addCategory(Intent.CATEGORY_OPENABLE);
   startActivityForResult(intent,1);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
    InputStream stream = null;
    if (requestCode == 1 && resultCode == RESULT_OK) {
        try {
            if (bitmap != null) {
                bitmap.recycle();
            }
            stream = getContentResolver().openInputStream(data.getData());
            bitmap = BitmapFactory.decodeStream(stream);
            imgl.setImageBitmap(bitmap);
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }
        finally {
            if (stream != null)
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }
}

Now just set the onClickListener img1 by calling the LoadGallery ().

NOTE: The code is large because I put all try / catch of possible errors. Test there and let me know if it worked. Thanks.

    
30.12.2014 / 02:56