我想通过意图将当前活动中的图像传递给新活动

时间:2016-11-16 03:20:52

标签: java android android-intent imageview

正如标题所示,我试图通过意图将当前活动RouteDetails.java中的图像传递给新活动FullScreenImage.java。我可以用putExtra getExtra做到这一点吗?有人可以给我代码来实现这个目标吗?我整天都在苦苦挣扎,我会非常感激。

继承我的RouteDetails.java

package com.example.zach.listview;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;


public class RouteDetails extends AppCompatActivity {

TouchImageView routeImage;
TouchImageView routeImageFull;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_route_details);

    //back button for route details view
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //TextView for route details
    final TextView routeDetailsView = (TextView) findViewById(R.id.routeDetailsView);
    routeDetailsView.setText(getIntent().getExtras().getString("route"));

    //ImageView for route details
    routeImage = (TouchImageView) findViewById(R.id.routeImage);
    routeImage.setImageResource(getIntent().getIntExtra("imageResourceId",     0));



////////// Trying to pass image to new activity

   routeImage.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view)
        {


            Intent intent = new Intent(view.getContext(), FullScreenImage.class);

            startActivity(intent);

        }

    });

////////////

}

//back button for route details view
@Override
public boolean onSupportNavigateUp(){
    finish();
    return true;
}

}

和我的activity_route_details.xml

<?xml version="1.0" encoding="utf-8"?>

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_route_details"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.zach.listview.RouteDetails">



<TextView
    android:text="TextView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/routeDetailsView"
    android:textSize="18sp"
    android:textAlignment="center" />

<com.example.zach.listview.TouchImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/routeImage"
    android:scaleType="fitCenter"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:adjustViewBounds="false"
    android:layout_below="@+id/routeDetailsView"/>

</RelativeLayout>
</ScrollView>

并且我想将FullScreenImage.java传递给

package com.example.zach.listview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


public class FullScreenImage extends AppCompatActivity {

TouchImageView routeImage;

@Override
protected void onCreate(Bundle saveInstanceState){
    super.onCreate(saveInstanceState);
    setContentView(R.layout.full_screen_image);

    //back button for route details view
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    routeImage = (TouchImageView) findViewById(R.id.fullScreenImageView);


}

//back button for route details view
@Override
public boolean onSupportNavigateUp(){
    finish();
    return true;
}



}

和full_screen_image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<com.example.zach.listview.TouchImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:srcCompat="@+id/routeImage"
    android:id="@+id/fullScreenImageView"
    android:layout_weight="1" />
</LinearLayout>

1 个答案:

答案 0 :(得分:0)

发件人活动

routeImage.buildDrawingCache();
Bitmap image= routeImage.getDrawingCache();

Bundle extras = new Bundle();
extras.putParcelable("imagebitmap", image);
intent.putExtras(extras);
startActivity(intent);

接收者活动

Bundle extras = getIntent().getExtras();
Bitmap bmp = (Bitmap) extras.getParcelable("imagebitmap");
routeImage.setImageBitmap(bmp );