Access photo library by android app

Asked

Viewed 2,141 times

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. There is also another image (Imageview1) and if you click on it, go to the photo library to choose a photo. After choosing the photo, update Imageview1. I was able to solve the part of accessing the camera by clicking on Imageview but I could not do the part of accessing the photo library, selecting a photo and replacing it with the current one. I believe that much of my code can be harnessed.

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>

1 answer

1


Luiz, good night to you! I was able to upload the images from my library, but it only worked for the images of what’s up (perhaps because of the size of the images. Anyway it follows the code:

First on 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 in img1 onClickListener calling the loadGallery().

Note: the code is big because I put all the Try/catch of possible errors. Test there and let me know if it worked. Hug.

  • It worked, thank you very much. The only problem is that I need to treat the access to the camera and later update a photo taken by it in an Imageview and also an access to library to select a photo and update another Imageview. I am not able to integrate these two processes within a single "onActivityResult". The code I made and shared only deals with the situation on camera. Another thing that I found strange, after taking the photo and selecting it, it updates Imageview but in a very small size, I did not find anything to return the photo of the size of Imageview @Magnusmaker

  • Just put one (if requestcode == 0) inside onActivityResult and set the treatment, because the call to take a photo is set to 0 in your code, the code I passed to open the gallery returns the requestcode == 1 (note that I put if requestcode==1 to handle my code), not to conflict understood? With regards to size I really don’t know yet how to solve...

  • I get it now, it makes sense. Again, thank you very much. I will validate as to the size change and if I find it, update here. Good evening @Magnusmaker

  • A further question has arisen. I do not know if you have identified this situation. I implemented the code you passed and I can access my image library. All photos are displayed and it is possible to select any photo and replace it with Imageview, EXCEPT the photos taken by the camera. An example of this is the fact that I used this code also to access the camera phone, take a photo and then add the photo in Imageview. When I take the photo and select it, it’s like I’m invisible in Imageview. @Magnusmaker

Browser other questions tagged

You are not signed in. Login or sign up in order to post.