2

in my code I use an intent to take a picture, but I really don't understand where I can get this image after that. My goal in this little application is to take a picture and then to update an ImageView with this picture (and to open google maps with an other intent). The two fonctions are activated with 2 buttons.

MainActivity.java :

import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    static final int REQUEST_IMAGE_CAPTURE = 1;

    public void activerCamera(View view) {
        Intent prendrePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        if (prendrePhoto.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(prendrePhoto, REQUEST_IMAGE_CAPTURE);
        }
    }

    public void googleMaps(View view) {
        EditText nameField = (EditText) findViewById(R.id.name_field);
        String name = nameField.getText().toString();
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse("geo:" + name));
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }
}

My activity_main.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
    tools:context="com.example.android.photoapp.MainActivity">


    <EditText
        android:id="@+id/name_field"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="Exemple : 47.6, -122.3"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="24dp"/>

    <Button
        android:layout_marginTop="24dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Aller sur google maps"
        android:layout_gravity="center_horizontal"
        android:textAllCaps="true"
        android:onClick="googleMaps"/>
    <Button
        android:layout_marginTop="24dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Prendre une photo"
        android:layout_gravity="center_horizontal"
        android:textAllCaps="true"
        android:onClick="activerCamera"/>

    <ImageView
        android:id="@+id/imageFondEcran"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

I saw in a lot of forum and tutorial some method like this (down), but i cannot make it work properly

@Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (data != null) {
        Uri photoUri = data.getData();
        // Do something with the photo based on Uri
        Bitmap selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
        // Load the selected image into a preview
        ImageView ivPreview = (ImageView) findViewById(R.id.ivPreview);
        ivPreview.setImageBitmap(selectedImage);   
    } }
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Hix
  • 33
  • 5
  • Why do you have no `onActivityResult` method in your class? You definitely need that. – OneCricketeer Feb 26 '17 at 21:23
  • How do you do that? That's not the same as `startActivityForResult` ? Where should I set that method `onActivityResult` ? – Hix Feb 26 '17 at 21:25
  • I'm just saying you called `startActivityForResult`, so where did you try to "catch" that result? – OneCricketeer Feb 26 '17 at 21:29
  • Someone answered down, and the point that I don't understand is : why it works? We are calling this method nowhere isn't? So why it works? – Hix Feb 26 '17 at 21:39

2 Answers2

2

I saw in a lot of forum and tutorial some method like this (down), but i cannot make it work properly

Since that code is wrong for most camera apps, I am not surprised that it is not working for you.

You are making an ACTION_IMAGE_CAPTURE request without including EXTRA_OUTPUT to say where the image should go. As a result, your photo should be a Bitmap, obtained by calling getParcelableExtra("data") on the Intent delivered to onActivityResult().

So, change:

if (data != null) {
    Uri photoUri = data.getData();
    // Do something with the photo based on Uri
    Bitmap selectedImage = MediaStore.Images.Media.getBitmap(this.getContentResolver(), photoUri);
    // Load the selected image into a preview
    ImageView ivPreview = (ImageView) findViewById(R.id.ivPreview);
    ivPreview.setImageBitmap(selectedImage);   
}

to:

if (requestCode==REQUEST_IMAGE_CAPTURE && resultCode==RESULT_OK) {
    Bitmap selectedImage = (Bitmap)data.getParcelableExtra("data");
    // Load the selected image into a preview
    ImageView ivPreview = (ImageView) findViewById(R.id.ivPreview);
    ivPreview.setImageBitmap(selectedImage);   
}
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Woaw I'm really impressed. It works totally.I don't really understand why the onActivityResult work, because there is no place where we call it. Can you explain this to me? (And thank you for your very quick answer ! I worked on it since hours ... ) Thanks ! – Hix Feb 26 '17 at 21:34
  • @Hix: "I don't really understand why the onActivityResult work, because there is no place where we call it" -- there are many callbacks in your activity that are called by the framework, not you. `onCreate()` is one example. `onActivityResult()` is another. `onActivityResult()` will be called as part of your activity returning to the foreground after having called `startActivityForResult()`. These sorts of callbacks are covered in any decent Android app development book or course. – CommonsWare Feb 26 '17 at 21:40
  • Okay, I see and it's a little less confuse. Do you know any website that describe these sorts of callbacks? I consulted the developper.android.com documentation, but I was not able to understand as I saw. – Hix Feb 26 '17 at 21:44
  • @Hix: I have not gone looking for one. However, those callbacks have not changed much in a while. Download a copy of [one of the older editions of my book](https://commonsware.com/Android/4-2-free). The most recent is about 2.5 years old, but it is free, and covers this among many other subjects. – CommonsWare Feb 26 '17 at 21:47
  • I see on your profile that you are very professionnal in Android developpement, so thanks for sharing your link, I will take a look ! – Hix Feb 26 '17 at 21:57
0

Try this:

In your activity you can call this to get your Drawable resource as a Drawable object:

Drawable d = getResources().getDrawable(R.drawable.yourimage);

If you want to show your drawable in an ImageView you can do this:

ImageView i = new ImageView(this);
i.setImageResource(R.drawable.yourimage);

or in your xml file:

<ImageView   
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/smiley"/>

Good luck!

Marcio
  • 1,685
  • 3
  • 24
  • 39
  • The person down you answer to my question, but I will definitively try it too ! Thanks a lot ! – Hix Feb 26 '17 at 21:35