2016-03-24 17 views
0

ImageSwitcherを他のアクティビティに送信しようとしましたが、ImageSwitcherの画像を別のアクティビティに送信しようとしました。ImageSwitcherは完全に機能しますが、 Views.javaAndroid ImageSwitcherの画像を別のアクティビティに変換する

MainEvent.java

import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.graphics.drawable.BitmapDrawable; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.Button; 
import android.widget.ImageSwitcher; 
import android.widget.ImageView; 
import android.widget.ViewSwitcher; 

import java.io.ByteArrayOutputStream; 

public class MainEvent extends Activity { 

private ImageSwitcher imageSwitcher; 
Button btnNext,back,select,refresh; 
ImageView imageView; 

// Array of Image IDs to Show In ImageSwitcher 
int imageIds[]={R.drawable.frame3,R.drawable.frame7, 
R.drawable.curtain,R.drawable.potraitimage 
     }; 
int messageCount=imageIds.length; 
// to keep current Index of ImageID array 
int currentIndex=0; 
Animation in,out; 
@Override 
protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.activity_main_event); 

    // get The references 
    btnNext=(Button)findViewById(R.id.button2); 
    back=(Button)findViewById(R.id.button); 
    select=(Button)findViewById(R.id.select); 
    refresh=(Button)findViewById(R.id.refresh); 
    imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher); 

// Set the ViewFactory of the ImageSwitcher that will create 
ImageView object when asked 
    imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() { 
     public View makeView() { 
      // TODO Auto-generated method stub 
      // Create a new ImageView set it's properties 
      imageView = new ImageView(getApplicationContext()); 
      imageView.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      imageView.setImageResource(R.drawable.potraitimage); 
      return imageView; 
     } 
    }); 

    // Declare the animations and initialize them 
    in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left); 
    out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right); 

    // set the animation type to imageSwitcher 
    imageSwitcher.setInAnimation(in); 
    imageSwitcher.setOutAnimation(out); 

    // ClickListener for NEXT button 
    // When clicked on Button ImageSwitcher will switch between Images 
    // The current Image will go OUT and next Image will come 
    in with specified animation 
    btnNext.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 

      // TODO Auto-generated method stub 
      imageView.setImageResource(0); 
      Log.d("index", String.valueOf(currentIndex)); 
      currentIndex++; 
      Log.d("index", String.valueOf(currentIndex)); 
      // If index reaches maximum reset it 
      if(currentIndex==messageCount) 
       currentIndex=0; 
      imageSwitcher.setImageResource(imageIds[currentIndex]); 
     } 
    }); 
    back.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // set the animation type to imageSwitcher 

      // TODO Auto-generated method stub 
      Log.d("index", String.valueOf(currentIndex)); 
      currentIndex--; 
      Log.d("index", String.valueOf(currentIndex)); 
      // If index reaches maximum reset it 
      if (currentIndex < 0) 
       currentIndex = 2; 
      imageSwitcher.setImageResource(imageIds[currentIndex]); 
     } 
    }); 
    select.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
     imageView.setImageResource(imageIds[currentIndex]); 
    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable(); 
      Bitmap bitmap = drawable.getBitmap(); 
      Intent in=new Intent(MainEvent.this,Camera.class); 
      /* in.putExtra("image",bitmap);*/ 
      ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
      byte[] byteArray = stream.toByteArray(); 
      Bundle bu=new Bundle(); 
      bu.putByteArray("ImageByte",byteArray); 
      in.putExtras(bu); 
      startActivity(in); 
     } 
    }); 
    refresh.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      onRestart(); 
     } 
    }); 
} 
@Override 
protected void onRestart() { 

    // TODO Auto-generated method stub 
    super.onRestart(); 
    Intent i = new Intent(MainEvent.this, MainEvent.class); //your class 
    startActivity(i); 
    finish(); 
} 
} 

Views.java

import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.ImageView; 

public class Views extends AppCompatActivity { 
ImageView image; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_camera); 
    image = (ImageView) findViewById(R.id.image); 
    Bundle extras = getIntent().getExtras(); 
    byte[] byteArray = extras.getByteArray("ImageByte"); 
    /* Intent i=getIntent();*/ 
    /* Bitmap bitmap=i.getExtras().getParcelable("image"); 
    image.setImageBitmap(bitmap);*/ 

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
    image.setImageBitmap(bmp);//here null pointer exception 

} 
} 

に私はIMAから画像を渡す方法がわかりませんあなたが私の手助けをしてください。

答えて

1

イメージのバイトを渡すのではなく、それがオンのインデックス(int)を渡すことはできませんか?

これはより効率的です。

(以下のチェーンからの拡張回答) 両方のアクティビティでアレイを共有します。静的プロパティで 簡単な方法:

public class StaticModelManager { 
    public static int imageIds[]; 
} 

次に、アプリケーション内のどこにでもそれを取得または設定:

StaticModelManager.imageIds = { 
    R.drawable.frame3, 
    R.drawable.frame7, 
    R.drawable.curtain, 
    R.drawable.potraitimage 
}; 

は、それらを使用します。

StaticModelManager.imageIds[currentIndex]; 

複数のアクティビティまたはフラグメントは、簡単にこの1にアクセスすることができますインスタンス。必要がないときやメモリに座っているときは、物事をきれいにするようにしてください。

+0

私はあなたを得ることができません? –

+0

イメージのインデックスがわかります。 // ImageID配列の現在のインデックスを保持する int currentIndex = 0; イメージのバイトの代わりにその整数を渡します。 他のアクティビティでは、同じ配列を使用し、インデックスを渡してイメージリソースを設定します。 ImageSwitcher INT imageIds [] = {R.drawable.frame3、R.drawable.frame7、 R.drawable.curtain、R.drawable.potraitimage }に表示する画像IDの //配列。 –

+0

私はそれを見ていきます。@shaunti fondrisi –