如何从图库(或可绘制)获取图像以更新ImageView?

时间:2017-02-26 21:18:49

标签: java android android-intent imageview

在我的代码中我使用了拍照的意图,但我真的不明白在那之后我可以在哪里获得这张照片。我在这个小应用程序中的目标是拍摄照片,然后用这张照片更新ImageView(并以其他意图打开谷歌地图)。这两个功能用2个按钮激活。

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);
        }
    }
}

我的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>

我在很多论坛和教程中看到过这样的一些方法(下),但我无法使其正常工作

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

2 个答案:

答案 0 :(得分:2)

  

我在很多论坛和教程中看到过这样的一些方法(下),但我无法使其正常工作

由于大多数相机应用程序的代码都是错误的,所以我并不感到惊讶它不适合你。

您正在发出ACTION_IMAGE_CAPTURE请求,但不包括EXTRA_OUTPUT以说明图片的去向。因此,您的照片应为Bitmap,通过致电getParcelableExtra("data") Intent来获取onActivityResult()

所以,改变:

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);   
}

为:

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);   
}

答案 1 :(得分:0)

试试这个:

在您的活动中,您可以调用此方法将Drawable资源作为Drawable对象:

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

如果您想在ImageView中显示您的drawable,您可以这样做:

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

或在您的xml文件中:

<ImageView   
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@drawable/smiley"/>
祝你好运!

相关问题