2012-11-27 149 views
6

在我的应用程序中,我从图库中显示no.of图像,只要我选择一个图像,应该将图像发送到所选图像将设置的新活动作为背景。然而,我能够从图库中获取图像,但只要我选择一个应用程序崩溃。提前感谢将位图图像从一个活动传递到另一个活动

活动-1(图像显示在图库中,所选图像发送到新活性)

public class Gallery extends Activity { 

private static int RESULT_LOAD_IMAGE = 1; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.gallery); 

     Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture); 


     buttonLoadImage.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View arg0) { 

       Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

       startActivityForResult(i, RESULT_LOAD_IMAGE); 
      } 
     }); 
    } 



    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 






      Uri contentUri = data.getData();   
      String[] proj = { MediaStore.Images.Media.DATA };   
      Cursor cursor = managedQuery(contentUri, proj, null, null, null);   
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);   
      cursor.moveToFirst();   
      String tmppath = cursor.getString(column_index);   
      Bitmap croppedImage = BitmapFactory.decodeFile(tmppath); 


      // Bitmap croppedImage = BitmapFactory.decodeFile(croppedImage); 
      Intent intent = new Intent(Gallery.this,GesturesActivity.class); 
      intent.putExtra("bmp",croppedImage); 
      startActivity(intent); 

      Log.v("sending image","sending image"); 


     } 


    } 
} 

活动-1(XML)

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/background" 
    > 
    <ImageView 
      android:id="@+id/imgView" 
      android:layout_width="fill_parent" 
      android:layout_weight="1" android:layout_height="wrap_content"></ImageView> 
    <Button 
      android:layout_height="wrap_content" 
      android:text="Load Picture" 
      android:layout_width="wrap_content" 
      android:id="@+id/buttonLoadPicture" 
      android:layout_weight="0" 
      android:layout_gravity="center"></Button> 
</LinearLayout> 

活动-2(其中,所选择的图像应该被设置为屏幕的背景图像的活动)

public class GesturesActivity extends Activity { 


     private final int MENU_CAMERA = Menu.FIRST; 


     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 


      Bitmap bmp = (Bitmap)this.getIntent().getParcelableExtra("bmp"); 
      BitmapDrawable background = new BitmapDrawable(bmp); 
      getWindow().setBackgroundDrawable(background); //background image of the screen 



      Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.advert); 
      View view = new SandboxView(this, bitmap); 

      setContentView(view); 
     } 



     public boolean onPrepareOptionsMenu(Menu menu) { 
      menu.clear(); 

       menu.add(0, 11, 0, "Take Snapshot"); 

        return super.onPrepareOptionsMenu(menu); 
     } 


     public boolean onOptionsItemSelected(MenuItem item) { 

      return super.onOptionsItemSelected(item); 
     } 



    } 
+0

检查[this](http://stackoverflow.com/a/6647023/1265724) –

+0

你可以发布logcat – Prachi

回答

18

有3个解决方案来解决这个问题。

1)首先将图像转换为字节数组,然后传入Intent,然后在下一个活动中从Bundle获取字节数组并转换为图像(位图)并设置为ImageView。

转换位图字节数组: -

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 
ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); 
byte[] byteArray = stream.toByteArray(); 

通行证字节数组到意图: - 从捆绑

Intent intent = new Intent(this, NextActivity.class); 
intent.putExtra("picture", byteArray); 
startActivity(intent); 

获取字节数组,转换成位图图像: -

Bundle extras = getIntent().getExtras(); 
byte[] byteArray = extras.getByteArray("picture"); 

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

image.setImageBitmap(bmp); 

2)首先将图像保存到SDCard中,然后在下一个活动中将此图像设置为ImageView。

3)将位图传递给Intent,并从bundle中获取下一个活动的位图,但问题是如果您的位图/图像大小很大,那么图像在下一个活动中不会加载。

+0

:根据你的第一个解决方案,如果我想转换一个图像到位图,我怎么能去做? –

1

我认为你可以做的定义位图静态和调用classname.bitmap你可以得到bitmap..and设置为下一个活动的背景

6

因为你是从库中检索图像,为什么不把ID传递给下一个活动,并在该活动中检索图像而不是传递图像?这将帮助你记忆和表现。

+3

+1,它是最方便的方式。存储用户选择的账户ID并将该ID传递给下一个活动。 –

0

IGP总结得很清楚,但在我看来,最有效的方法是将图像的URI传递给下一个活动,而不是Bitmap本身。实际上,我不确定是否可以使用Intents将一个活动的数据传递到另一个活动的整个Bitmaps(或转换后的ByteArrays) - 我相信Bundle可以包含多少数据是有限制的。

而是传递您用来在第一个活动中显示图像的引用。我假设你正在使用某种延迟加载?如果不是,我强烈建议你这样做。这样你就可以简单地通过URI重新查询位图。

但是,我能得到从图库中的图片,但只要我 选择一个应用程序崩溃

我仍然困惑的是这些类型的问题是如何达到SO。检查日志,也许你可以自己计算出来。

0

使用此功能可以将位图传递给其他活动。

如果您使用drawable,则首先将该drawable转换为位图。

Bitmap bitmap = ((BitmapDrawable)d).getBitmap(); 

对于使用intent将该位图传递给另一个活动,请使用以下代码片段。

intent.putExtra("Bitmap", bitmap); 

而对于取另一个动作是位图的意图使用该

Bitmap bitmap = (Bitmap)this.getIntent().getParcelableExtra("Bitmap"); 

关注这个 Link for More Detail.

5

位图实现Parcelable,所以你总是可以通过它的意图。请尝试以下代码:

第一个活动。

Intent mIntent = new Intent(this, ActivityTwo.class); 
mIntent.putExtra("bmp_img", bmp); 

在目标活动获得输出,尝试:

次活动。

Bitmap mBitmap = (Bitmap) intent.getParcelableExtra("bmp_img"); 

你总是使用getParcelableExtra( “密钥”)在活动获得通过位图。

+2

如果位图太大,此代码可能会失败 –

+1

@oren大型位图的替代选择是什么? –

+0

@AnshulTyagi如果你正在处理大的位图,你可以将它们保存到内部存储器中,将文件名额外传递给从内部存储器读取它的第二个活动。我用它来传输屏幕截图,效果很好。 – cuddlecheek

0

我发现最简单的(但绝对不是最优雅的)方法是使用静态类成员。例如:

class PassedData 
{ 
    public Bitmap bm1, bm2, etc; 

    private PassedData current; 

    public static PassedData getCurrent() {return current;} 

    public PassedData() 
    { 
     current = this; 
    } 
} 

然后,每个活动都可以引用PassedData.getCurrent()。

0

活动

要传递Activites

Intent intent = new Intent(this, Activity.class); 
intent.putExtra("bitmap", bitmap); 

Bitmap bitmap = getIntent().getParcelableExtra("bitmap"); 
之间以及在活动类的位图

片段

要通过片段

SecondFragment fragment = new SecondFragment(); 
Bundle bundle = new Bundle(); 
bundle.putParcelable("bitmap", bitmap); 
fragment.setArguments(bundle); 

之间的位图来接收SecondFragment

Bitmap bitmap = getArguments().getParcelable("bitmap"); 

如果您收到失败粘结剂TR内ansaction,这意味着您将大元素从一个活动转移到另一个活动,从而超越了活页夹事务缓冲区。

因此,在这种情况下,你要压缩的位图作为一个字节的数组,然后在另一个活动解压,这样

在FirstActivity

Intent intent = new Intent(this, SecondActivity.class); 

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream); 
byte[] bytes = stream.toByteArray(); 
intent.putExtra("bitmapbytes",bytes); 

而在SecondActivity

byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes"); 
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length); 
相关问题