android - 如何将图库中的图像上传到 ImageView 中?

标签 android android-intent android-imageview android-image onactivityresult

所以我正在尝试将我从图库中挑选的图像上传到我拥有的 ImageView 中。但是图像从未出现在 ImageView 中?该程序不会崩溃或发生任何事情。我使用 toast 消息来查看 onActivityResult 是否捕获了图像,它确实捕获了。但它无法将其加载到 ImageView 中。

希望大家能帮忙,下面是我的代码。

public class TakePhotoActivity extends AppCompatActivity implements View.OnClickListener {
    private View snapButton;
    private ImageButton galleryButton;
    private int RESULT_LOAD_IMAGE = 1;
    private String user_image_path;
    private boolean pickedCamera = false;
    private ImageView myImageView;
    private ProgressBar myprogressBar;

    //GPS
    private LocationManager locationManager = null;
    private LocationListener locationListener = null;

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

    private void setupCamera()
    {
        snapButton = (View) findViewById(R.id.cameraActivity_CameraButton);
        snapButton.setOnClickListener((View.OnClickListener) this);
        snapButton.setOnClickListener(this);
        galleryButton = (ImageButton) findViewById(R.id.cameraActivity_GalleryButton);
        galleryButton.setOnClickListener(this);
        myprogressBar = (ProgressBar) findViewById(R.id.myPB);
        myprogressBar.setVisibility(View.INVISIBLE);

        myImageView = (ImageView) findViewById(R.id.cameraActivity_ImageView);


    }

    private void grabImageFromGallery()
    {
        Intent imageGetter = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(imageGetter, RESULT_LOAD_IMAGE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
        {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string
            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            user_image_path = cursor.getString(columnIndex);//here we have our image path.
            cursor.close();

        }
        Picasso.with(this).load(user_image_path).fit().centerCrop().into(myImageView);


    }

    @Override
    public void onClick(View v)
    {
        switch (v.getId())
        {
            case R.id.cameraActivity_GalleryButton:
                grabImageFromGallery();
                break;
        }
    }


}

这是我的 xml。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        layout="@layout/toolbar_layout">
    </include>

        <ImageView
            android:layout_width="200dp"
            android:layout_height="150dp"
            android:id="@+id/cameraActivity_ImageView"
            android:layout_marginLeft="100dp"
            android:layout_marginTop="50dp"/>




    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <ImageButton
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/circle_fb_2"
            android:src="@drawable/icon_camera_red"
            android:layout_marginTop="70dp"
            android:layout_marginLeft="70dp"
            android:id="@+id/cameraActivity_CameraButton"/>

        <ImageButton
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/circle_fb_2"
            android:src="@drawable/icon_gallery"
            android:layout_marginTop="70dp"
            android:layout_marginLeft="150dp"
            android:layout_alignLeft="@id/cameraActivity_CameraButton"
            android:id="@+id/cameraActivity_GalleryButton"/>

        <ProgressBar
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:id="@+id/myPB"
            android:layout_below="@id/cameraActivity_CameraButton"
            android:layout_marginLeft="170dp"/>
    </RelativeLayout>



</LinearLayout>

最佳答案

加载本地镜像时,您的路径应该像这样“file:///image_path.jpg”。

替换

Picasso.with(this).load(user_image_path).fit().centerCrop().into(myImageView);

Picasso.with(this).load("file:///"+user_image_path).fit().centerCrop().into(myImageView);

关于android - 如何将图库中的图像上传到 ImageView 中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37540659/

相关文章:

java - 您如何从 Parse 在 Android 中的两个不同 Activity 之间进行查询?

javascript - Youtube API 检测自动播放是否被禁用

android - Intent.ACTION_DIAL,数字以#结尾

android - 非内置应用程序的隐式 Intent

android - CollapsingToolbarLayout - 调暗/模糊背景图像

android - Fabric for Android 和 twitter4j 都找不到包含特定用户提及的推文

java - Android App判断是否有连接

java - 以字符串形式将联系人作为 vCard 启动 Intent

android - 自定义布局 Android 中的图标大小

android - 使用 Thumbnailutils 显示缩略图时出现问题