如何将图像从一个活动保存到另一个活动

时间:2016-04-12 21:01:53

标签: android android-activity

所以我从一个用于构建图像的屏幕开始。 my build mode 当我点击模板时,我会进入一个看起来像stencil picker的新活动 在我的模板选择器中,我有一个onclick方法用于使用开关盒的图片。我需要做的是记住我点击了什么图像并将该图像应用到上一页。我还需要能够保存添加到构建模式的图像,以便它们可以堆叠在彼此之上(但这是次要问题)。 这是构建页面的代码

public class BuildMode extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener, AdapterView.OnItemSelectedListener {

private Button mStencilButton;
//private Button mColorButton;
private Button mUndoButton;
private Button mRedoButton;
private Spinner mColorSpinner;
private ImageView mImageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_build_mode);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    //bit map to get stencil selected and place into build mode
   // Bundle extras = getIntent().getExtras();
  //  byte[] byteArray = extras.getByteArray("picture");

  //  Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
  //  ImageView image = (ImageView) findViewById(R.id.egg_image);

  //  image.setImageBitmap(bmp);



    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);

    mStencilButton = (Button) findViewById(R.id.stencil_button);
    // mColorButton = (Button) findViewById(R.id.color_button);
    mUndoButton = (Button) findViewById(R.id.undo_button);
    mRedoButton = (Button) findViewById(R.id.redo_button);
    mColorSpinner = (Spinner) findViewById(R.id.colors_spinner);

    mColorSpinner.setOnItemSelectedListener(this);

    mStencilButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(BuildMode.this, StencilList.class);
            startActivity(intent);

        }
    });


    mUndoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });
    mRedoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

        }
    });

    mColorSpinner = (Spinner) findViewById(R.id.colors_spinner);
    mColorSpinner.setOnItemSelectedListener(this);
}


@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.build_mode, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.about_us) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_manage_build) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery_build) {
        Intent intent = new Intent(BuildMode.this, PreviewMode.class);
        startActivity(intent);

    }


    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    switch (position) {
        case 0:
           // mImageView.setBackgroundColor(Color.WHITE);
            break;
        case 1:
          //  mImageView.setBackgroundColor(Color.YELLOW);
            break;
        case 2:
          //  mImageView.setBackgroundColor(Color.GREEN);
            break;
        case 3:
          //  mImageView.setBackgroundColor(Color.BLUE);
            break;
        case 4:
          //  mImageView.setBackgroundColor(Color.RED);
            break;
        case 5:
        //    mImageView.setBackgroundColor(Color.BLACK);
            break;

    }


}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}

}

和我的模板页面代码

public class StencilList extends AppCompatActivity {
private ImageView mStencil1;
private Bitmap bmp;
private byte[] byteArray;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_stencil_list);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
}
public void useStencil(View view) {
    // Intent intent = new Intent(this, BuildMode.class);
    switch (view.getId()) {
        case R.id.stencil_image_1:
            //      intent.putExtra("picture", byteArray);
            //     startActivity(intent);
            break;
        case R.id.stencil_image_2:
            break;
        case R.id.stencil_image_3:
            break;
        case R.id.stencil_image_4:
            break;
        case R.id.stencil_image_5:
            break;
        case R.id.stencil_image_6:
            break;
        case R.id.stencil_image_7:
            break;
        case R.id.stencil_image_8:
            break;
        case R.id.stencil_image_9:
            break;
        case R.id.stencil_image_10:
            break;
        case R.id.stencil_image_11:
            break;
        case R.id.stencil_image_12:
            break;
        case R.id.stencil_image_13:
            break;
        case R.id.stencil_image_14:
            break;
    }

}

private void imageClicked()
{

}

}

1 个答案:

答案 0 :(得分:0)

我会试试这个:

  • 当您点击模板选项时,您需要一种方法来了解用户点击的位置
  • 也许,您可以轻松创建一个变量来设置何时单击模具,然后将其作为Extra传递给您之前的活动
  • 进入其他活动后,请保留图像绘图的简单整数数组。
  • 最后,使用通过意图收到的索引获取相应的图像。那应该有用!

我希望这有帮助!