2011-11-24 94 views
0

我想做一个应用程序有2 ActivitiesActivity1是一个画廊可以拨打Activity2Activity2是一款捕捉图像的相机应用程序,然后将这些图像发送到Activity1Activity1将在gallery.I无法发送图像之间两个Activities之间的图像。如何将图像从活动发送到其他活动?

此代码是我的画廊Activity1。我知道SerializebleBitmap,但在我的代码,我怎么能把它放在画廊列表?这里是我的代码:

public class HelloGallery extends Activity { 
private Gallery gallery; 
private ImageView imgView; 
private Button btn; 
private static final int SHOW_SUB_FORM = 0; 
private String[] imglist; 
Bundle extras = getIntent().getExtras(); 
private Integer[] mImageIds = { 
     R.drawable.sample_0, 
     R.drawable.sample_1, 
     R.drawable.sample_2, 
     R.drawable.sample_3, 
     R.drawable.sample_4, 
     R.drawable.sample_5, 
     R.drawable.sample_6, 
     R.drawable.sample_7 
}; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); 
    gallery = (Gallery) findViewById(R.id.gallery); 
    gallery.setAdapter(new ImageAdapter(this)); 
    imgView = (ImageView)findViewById(R.id.ImageView01); 
    imgView.setImageResource(mImageIds[0]); 

    btn = (Button) findViewById(R.id.Takept); 


     btn.setOnClickListener(new View.OnClickListener(){ 




     public void onClick(View v) { 

      btn_onClick(); 
     } 

    }); 



    if (extras != null){ 

     String[] imglist= extras.getStringArray("IMAGE_LIST"); 
     imgView.setImageResource(mImageIds[0]); 


    } 

    gallery.setOnItemClickListener(new OnItemClickListener() { 
     public void onItemClick(AdapterView parent, View v, int position, long id) { 
      Toast.makeText(HelloGallery.this, "" + position, Toast.LENGTH_SHORT).show(); 
      imgView.setImageResource(mImageIds[position]); 




     } 
    }); 


}; 

private void btn_onClick() { 

    Intent intent = new Intent(HelloGallery.this,CameraDemo.class); 


    startActivity(intent); 
} 

public class ImageAdapter extends BaseAdapter { 
    int mGalleryItemBackground; 
    private Context mContext; 


    public ImageAdapter(Context c) { 
     mContext = c; 
     TypedArray attr = mContext.obtainStyledAttributes(R.styleable.HelloGallery); 
     mGalleryItemBackground = attr.getResourceId(
       R.styleable.HelloGallery_android_galleryItemBackground, 0); 
     attr.recycle(); 
    } 

    public int getCount() { 
     return mImageIds.length; 
    } 

    public Object getItem(int position) { 
     return position; 
    } 

    public long getItemId(int position) { 
     return position; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView = new ImageView(mContext); 

     imageView.setImageResource(mImageIds[position]); 
     imageView.setLayoutParams(new Gallery.LayoutParams(100, 100)); 
     imageView.setScaleType(ImageView.ScaleType.FIT_XY); 
     imageView.setBackgroundResource(mGalleryItemBackground); 

     return imageView; 
    } 
} 
} 
+0

可能的重复:http://stackoverflow.com/questions/4497813/how-to-communicate-two-android-applications – Drahakar

+0

这里将图像转换为位图,并将其传递给另一个活动 [使用此答案] [1] [1]:http://stackoverflow.com/a/17879210/2377760 –

回答

0

一般情况下,你不希望从一个活动发送图像到另一个。你会希望活动1将它存储在某个地方,然后告诉活动2去访问它。可能在两个活动都可以访问的另一个基本应用程序类中。

希望这有助于

0

在你的意图,你有可能通过Serializable对象。

做对,你必须创建serilizing类/反序列化你的形象是这样的:

import java.io.ByteArrayOutputStream; 
    import java.io.IOException; 
    import java.io.Serializable; 

    import android.graphics.Bitmap; 
    import android.graphics.BitmapFactory; 
    import android.graphics.Bitmap.CompressFormat; 

    public class SerializableBitmap implements Serializable { 
     private static final long serialVersionUID = 1208813848065674061L; 
     private Bitmap bitmap; 
     // Serialize image 
     private void writeObject(java.io.ObjectOutputStream out) throws IOException { 
      ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
      bitmap.compress(CompressFormat.JPEG, 100, bos); 
      byte[] bitmapdata = bos.toByteArray(); 
      out.write(bitmapdata, 0, bitmapdata.length); 
     } 

     // Deserialize image 
     private void readObject(java.io.ObjectInputStream in) throws IOException, ClassNotFoundException { 
      ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); 
      int b; 
      while((b = in.read()) != -1) 
       byteStream.write(b); 
      byte bitmapBytes[] = byteStream.toByteArray(); 
      bitmap = BitmapFactory.decodeByteArray(bitmapBytes, 0, bitmapBytes.length); 
     } 

     public Bitmap getBitmap() { 
      return bitmap; 
     } 

     public void setBitmap(Bitmap bitmap) { 
      this.bitmap = bitmap; 
     } 
    } 

填写免费使用setBitmap()和getBitmap()方法来序列化/ deserilize您的位图,并在意图把序列化的位图对象:

intent.putExtra(extraDataKey, serializableBitmap); 

但考虑到这种解决方案可能会导致性能问题如果图像是太大。

希望它有帮助。

+0

[位图(http://developer.android.com/ reference/android/graphics/Bitmap.html)已经实现了[Parcelable](http://developer.android.com/reference/android/os/Parcelable.html),直接将位图传递给intent.putExtra(),不要需要重新包装一次。 – yorkw

+0

然后我如何才能在activity1的galleyry上显示这些图像? –

+0

@ user1063009位图图像=(位图)getIntent()。getExtras()。getParcelable(extraDataKey); – yorkw

0

您发送像“R.drawable.sample_0”这样的图像的位置作为字符串发送,然后当您在活动2中获得它时,您从绘图中得到它。这一切都不需要发送图像:)

相关问题