0

I am developing an application to access the mobile photo library and upload a picture after selecting. The application is loading all the photos perfectly, except pictures taken by the camera. Someone might try to help me?

MainActivity.java

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.ImageView;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;


public class MainActivity extends ActionBarActivity {

ImageView img;
private Bitmap bitmap;

@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) {
            abrirFoto();
        }

    });
}

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

@Override
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);
            img.setImageBitmap(bitmap);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (stream != null)
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

    }
}
}

activity_main.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"
android:background="#0B9AE2">

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView"
    android:src="@drawable/fotos1"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_weight = "1"/>

<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/textView2"
    android:layout_centerHorizontal="true"
    android:layout_weight = "1"/>

</RelativeLayout>
Luiz Henrique Ugliano
  • 1,317
  • 2
  • 13
  • 26
  • Possible duplicate of [How to access an image from the phone's photo gallery?](https://stackoverflow.com/questions/11144783/how-to-access-an-image-from-the-phones-photo-gallery) – Edward Brey May 30 '19 at 14:30

2 Answers2

0

As i read your question, i understand that you are trying to capture a photo using camera and trying to set it to imageview. Correct me if i am wrong.

To upload a picture from camera, you have to try a different intent.

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
startActivityForResult(cameraIntent, CAMERA_REQUEST); 

In your onActivityResult method, add the below lines of code.

if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            img.setImageBitmap(photo);
        }  
Prem
  • 4,823
  • 4
  • 31
  • 63
  • My code accesses the mobile library of images, images that exist in memory. My question is that when I access the photo library on my phone, any picture that I select updated ImageView except the photos taken by the camera off. If you run my code will understand perfectly. Thank you. @Prem – Luiz Henrique Ugliano Jan 07 '15 at 00:53
0

if i get it right you are trying to take a photo with your device camera and use that photo in your application, well there are many examples online for that purpose but here i'll give you something that me think it is easy to understand at least that what i think here is the code: 1-define those as global variables:

Intent i;
final static int cameraData=0; 
Bitmap bmp;

2-in your on click method or what ever method you are using to take a photo use this:

i=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(i,cameraData);

3-call onActivityResult() method:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        Bundle extras=data.getExtras();
        bmp = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);
    }
}

here you go, simplest that i could find hope it will work for you. Good luck

Ahmad Sanie
  • 3,678
  • 2
  • 21
  • 56
  • My code accesses the mobile library of images, images that exist in memory. My question is that when I access the photo library on my phone, any picture that I select updated ImageView except the photos taken by the camera off. If you run my code will understand perfectly. Thank you. @Ahmad Alsanie – Luiz Henrique Ugliano Jan 07 '15 at 00:53