将图像从一个活动发送到另一个活动失败

时间:2014-11-22 11:19:00

标签: android android-activity bundle

我正在尝试整合google plus登录以获取用户详细信息,例如姓名,电子邮件和个人资料照片。

现在使用下面的代码我试图获取他的名字,电子邮件和个人资料图片,如果我在同一个活动中使用它我也得到他的个人资料照片

Login.Java

public void onConnected(Bundle connectionHint) {

    // We've resolved any connection errors. mGoogleApiClient can be used to
    // access Google APIs on behalf of the user.
    // Get user's information
    getProfileInformation();
}

private void getProfileInformation() {

    if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {

        Intent i = new Intent(getApplicationContext(), MainActivity.class);
        Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);
        String personPhotoUrl = currentPerson.getImage().getUrl();
        String personGooglePlusProfile = currentPerson.getUrl();
        Toast.makeText(this, personPhotoUrl, Toast.LENGTH_LONG).show();
        String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
        //new GetProfileImage(urImageView).execute(personPhotoUrl);
        // Create the bundle
        new GetProfileImage().execute(personPhotoUrl);
        Bundle bundle = new Bundle();
        // Add your data from getFactualResults method to bundle
        bundle.putString("Google", "Logged in using Google Account");
        bundle.putString("GoogleUsername", currentPerson.getDisplayName());
        bundle.putString("GoogleEmail", email);
        if(resultBmp!=null) {

            i.putExtra("GoogleProfileImage", resultBmp);
        }
        i.putExtras(bundle);
        startActivity(i);
    }

    private class GetProfileImage extends AsyncTask<String, Void, Bitmap> {

        protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {

            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);

        } catch (Exception e) {

            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {

        resultBmp = result;
        //bmImage.setImageBitmap(result);
    }
}

MainActivity.Java

protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(Plus.API, PlusOptions.builder().build())
        .addScope(Plus.SCOPE_PLUS_LOGIN)
        .build();

    mGoogleApiClient.connect();
    Intent intent = getIntent();
    if(intent.getStringExtra("Google") != null){ 

        // 1. get passed intent 
        // 2. get message value from intent
        String userName = intent.getStringExtra("GoogleUsername");
        String email = intent.getStringExtra("GoogleEmail");
        if(intent.getStringExtra("Google").equals("Logged in using Google Account")){

            ((TextView)findViewById(R.id.txtUser)).setText(userName);
            ((TextView)findViewById(R.id.txtemail)).setText(email);

            Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("GoogleProfileImage");
            //Bitmap bitmap = getIntent().getParcelableExtra("GooglePic");
            ImageView imageView = (ImageView) findViewById(R.id.imgProfilePic);
            imageView.setImageBitmap(bitmap);
        }
    }
}

protected void onStart() {

    super.onStart();
    mGoogleApiClient.connect();
}

protected void onStop() {

    super.onStop();
    if (mGoogleApiClient.isConnected()) {

        mGoogleApiClient.disconnect();
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {

    // TODO Auto-generated method stub
    Log.d("Debug","Connection failed");
    Intent i = new Intent(this,Login.class);
    startActivity(i);
    finish();
    //super.onConnectionFailed(result);
}

@Override
public void onConnected(Bundle connectionHint) {

    // TODO Auto-generated method stub
    Log.d("Debug","Connected");
    //super.onConnected(connectionHint);
    mGoogleApiClient.connect();
}

如果我尝试将此图像发送到下一个活动,它会在第一次登录时向我显示图片。如果我第二次登录或者我恢复应用程序,它会显示图片。

有人能说我在MainActivity中哪里出错吗?

3 个答案:

答案 0 :(得分:0)

尝试这样,

在将其添加到intent之前将其转换为Byte数组,将其发送出去并解码。

//转换为字节数组

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image",byteArray);

然后在活动2中:

byte[] byteArray = getIntent().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);

答案 1 :(得分:0)

使您的位图引用静态可以从另一个活动访问静态变量。

答案 2 :(得分:0)

我的问题是:

加载图片需要花费时间,因此当活动第一次开始时图像尚未加载时,您只能在第二次加载图像时看到图像。

尝试使用asyncTask或任何其他后台进程来加载图像。

如果您使用普通线程,请不要忘记拨打&#34; runOnUIthread&#34;设置图像时查看。 如果您已经使用后台进程加载图像,则在完成该过程时将回调用于调用。