2015-07-13 91 views
0
import android.content.Intent; 
import android.graphics.Bitmap; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ImageView; 
import java.io.ByteArrayOutputStream; 


public class MainActivity extends ActionBarActivity { 

    Bitmap bitmap; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ImageView i = (ImageView) findViewById(R.id.iv_img); 
    i.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      ByteArrayOutputStream bStream = new ByteArrayOutputStream(); 
      bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream); 
      byte[] byteArray = bStream.toByteArray(); 

      Intent anotherIntent = new Intent(MainActivity.this, anotherActivity.class); 
      anotherIntent.putExtra("image", R.drawable.img); 
      startActivity(anotherIntent); 

     } 
    }); 
} 
+2

传递图像的路径并使用另一个活动中的路径检索图像。 – Raghunandan

+0

将文件写入文件系统(sdcard)并将路径传递给另一个活动,在下一个活动中读取文件并删除本地路径副本(如果不再需要)。 –

+1

其可绘制的,你可以在另一个活动中访问它。 – Raghunandan

回答

0

您可以使用intent将byteArray发送到其他活动。

Intent anotherIntent = new Intent(MainActivity.this, anotherActivity.class); 

anotherIntent.putExtra("image", byteArray); 

startActivity(anotherIntent); 

现在你必须接受这个bytearray到另一个活动。

Bitmap bmp; 

byte[] byteArray = getIntent().getByteArrayExtra("image"); 

bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 

imageview.setImageBitmap(bmp); 
相关问题