将图像从一个类发送到另一个类(活动到活动)

时间:2018-04-21 00:03:32

标签: java android arrays image if-statement

我在下面附上了我的代码。

这一刻非常粗糙,我努力让它发挥作用。

基本上,用户从页面中选择一个图像,然后它将加载到另一个(锻炼中心)。

这是我发送信息的地方(图片)我基本上希望用户能够选择图像并动态地将图像视图添加到第二个活动中(这是来自第一个活动)

 /**
     *
     */
    protected void loadPullUps(){

        Intent loadPullUpImage= new Intent(this, workout_loader.class);
        //loadPullUpImage.putExtra("pull-ups", byteArray);
        loadPullUpImage.putExtra("pull_ups_var", true);
        startActivity(loadPullUpImage);

    }

    /**
     *
     */
    protected void loadReverseRows(){

        Intent loadReverseRowImage= new Intent(this, workout_loader.class);
        //loadPullUpImage.putExtra("pull-ups", byteArray);
        loadReverseRowImage.putExtra("reverse_rows_var", true);
        startActivity(loadReverseRowImage);

    }

    /**
     *
     */
    protected void loadRackPulls(){


    }

这是我的第二个活动,我将把图片加载到我希望用户选择出现的位置。第一个loadPullups()工作,但是如果我选择另一个并且代码通常很糟糕,它就会崩溃。我今天花了大约14个小时试图让它工作,我正面临一堵墙。

     protected void loadWorkout(){

     /*   int[] myImageList = new int[]{R.drawable.pull_ups, R.drawable.rack_pulls, R.drawable.reverse_rows,
                R.drawable.cable_curls, R.drawable.ez_bar_curls, R.drawable.bicep_curls_exercise, R.drawable.chest_press,
                R.drawable.cable_crossovers, R.drawable.push_ups, R.drawable.tricep_pulldowns, R.drawable.tricep_kickbacks,
                R.drawable.tricep_dips, R.drawable.dumbbell_press, R.drawable.shoulder_shrugs, R.drawable.shoulder_flyes,
                R.drawable.barbell_squat, R.drawable.leg_extensions, R.drawable.leg_press};

*/
        Bundle extras = getIntent().getExtras();



        LinearLayout layout = (LinearLayout)findViewById(R.id.container);
       /*for(int i=0;i<10;i++)
        {
            ImageView image = new ImageView(this);
            image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80,60));
            image.setMaxHeight(500);
            image.setMaxWidth(200);

            // Adds the view to the layout
            image.setImageResource(R.drawable.pull_ups);

            layout.addView(image);
        }
*/



            if (extras.getBoolean("pull_ups_var")) {
                Log.e("AFTER\t", " " + extras.getBoolean("pull_ups_var"));
                int[] images = {R.drawable.pull_ups};
                int cImage = images[0];
                imgView1.setImageResource(cImage);
            }
            else if (extras.getBoolean("reverse_rows_var")) {
                Log.e("AFTER\t", " " + extras.getBoolean("reverse_rows_var"));
                int[] images = {R.drawable.reverse_rows};
                int cImage = images[2];
                imgView2.setImageResource(cImage);
            }
        }


    }

1 个答案:

答案 0 :(得分:0)

您无法将图像从一个活动发送到另一个活动。您将获得TransactionTooLarge例外。交易数据的最大限制大约为1 MB,您的图像很容易超过它。

您应该发送图像的URI并在另一个活动中重建。

相关问题