简体   繁体   中英

Image doesn't load

I have been working on the option for a user to add a picture to his profile in an application. This is what happens so far:

You can press the default picture and it will take you to (in this case) dropbox. You then select a picture and load it in (this would be beautiful if it worked). The problem that occurs is when I selected a picture that I want, it should load into the new profile picture. But the profile picture is not replaced by the one I selected. Below you will see the code I am using in my profile .class

package com.example.wilmar.rentacube.Profile;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.wilmar.rentacube.R;

/**
 * Created by wilmar on 23-4-2015.
 */
public class Profile extends Activity {

    ImageView contactImageImgView;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.profile);



        contactImageImgView = (ImageView) findViewById(R.id.imgViewContactImage);


        contactImageImgView.setOnClickListener(new View.OnClickListener() { // this is where the magic happens (or is supposed to)

            public void onClick (View v){  
                Intent intent = new Intent();
                intent.setType("image*/");
                intent.setAction(intent.ACTION_GET_CONTENT);
                startActivityForResult(intent.createChooser(intent, "Select Profile Image"), 1);
            }
        });

    }




// here should happen some more magic :)
    public void onActivityResult(int reqCode, int resCode, Intent data) {
        if(resCode == RESULT_OK){
            if(resCode == 1)
                contactImageImgView.setImageURI(data.getData());
        }

    }


} 

First of all, this

if(resCode == 1)
contactImageImgView.setImageURI(data.getData());

should be

if( reqCode == 1)    //You used the wrong value here
contactImageImgView.setImageURI(data.getData());

Also, every app you can send an intent to will return a slightly different URI. Your best bet is to log it and see if it has no "garbage" attached.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM