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

Android 将映像从一个活动发送到另一个活动失败,android,android-activity,bundle,Android,Android Activity,Bundle,我正在尝试整合google plus登录,以获取用户详细信息,如姓名、电子邮件和他的个人资料 现在使用下面的代码,我试图得到他的名字,电子邮件和个人资料图片,如果我在同一活动中使用它,我也得到他的个人资料图片 Login.Java public void onConnected(Bundle connectionHint) { // We've resolved any connection errors. mGoogleApiClient can be used to //

我正在尝试整合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);
    }
}
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();
}
如果我试图将此图像发送到下一个活动,它不会在第一次登录时显示图片。如果我第二次登录或我恢复应用程序,它会显示图片

有人能告诉我我的主要活动哪里出了问题吗?

试着这样做

将其转换为字节数组,然后再将其添加到intent、发送并解码

//转换为字节数组

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

将位图引用设为静态,因为可以从其他活动访问静态变量。

我想您的问题在于:

加载图像需要时间,因此,当活动第一次启动时,图像尚未加载,这是您仅在第二次加载图像时才看到图像的方式

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

如果您使用普通线程,请不要忘记在将图像设置为查看时调用“runOnUIthread”。
如果您已经使用后台进程加载图像,请在进程完成时设置回调以调用。

我认为更好的方法是在MainActivity中传递imageview路径并从路径获取图像,传递图像bitamp需要大量内存。是否有任何理由传递图像位图而不是路径?没有类似的情况。我只想在用户登录gmail和youtube并使用导航抽屉后显示其个人资料图片。我认为您必须使用AndroidQuery库以异步方式加载图像,并提供选项缓存图像因此,当您登录时,活动映像将从url加载,并在您只传递url或路径时立即缓存,它将直接从缓存区域而不是从url源加载。创建字节数组不是正确的解决方案,因为从映像创建字节需要时间,这可能会导致下一屏幕显示延迟。当您通过Intent传递对象时,可以使用parcelable接口。请从developer.android阅读有关parcelable的更多信息。com@Moradiya-谢谢你的帮助。我也试过这种方法,但没有成功