1.第一种方式就是使用Intent,通过parcelable方式传递
//Activity One
Intent intent = new Intent(this, Two.class);
intent.putExtra("bp", bitmap);
//Activity Two
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("bp");
2.第二种方式
也是使用Intent,通过字节数组传递
//Activity One
Intent intent = new Intent(this, Two.class);
intent.putExtra("bp", bitmap);
//Activity Two
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("bp");
<span style="font-size:18px;">//Activity One
Intent intent = new Intent(this, Two.class);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bs);
byte[] bitmapByte = bs.toByteArray();
intent.putExtra("bp", bitmapByte);
this.startActivity(intent);</span>
<span style="font-size:18px;">//Activity Two
byte[] byteTemp = intent.getByteArrayExtra("bitmap");
bitmap = BitmapFactory.decodeByteArray(byteTemp, 0, byteTemp.length);</span>
3.第三种方式,是以流的形式写入到文件到,通过Intent传文件路径到另一个Activity
<span style="font-size:18px;">//Activity One
Intent intent = new Intent(this, Two.class);
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bs);
byte[] bitmapByte = bs.toByteArray();
intent.putExtra("bp", bitmapByte);
this.startActivity(intent);</span>
<span style="font-size:18px;">//Activity Two
byte[] byteTemp = intent.getByteArrayExtra("bitmap");
bitmap = BitmapFactory.decodeByteArray(byteTemp, 0, byteTemp.length);</span>
package com.example.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
public class BitmapUtil
{
/**
* 保存Bitmap为文件;可能报空指针是因为没有配置权限
*
* @param bmp
* @param filename
* @return
*/
public static void saveBitmap2file(Bitmap bmp, String filename)
{
CompressFormat format = Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;
try
{
stream = new FileOutputStream(Environment
.getExternalStorageDirectory().getPath()
+ "/"
+ filename
+ ".jpg");
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
bmp.compress(format, quality, stream);
try
{
stream.flush();
stream.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 读取文件为Bitmap
*
* @param filename
* @return
* @throws FileNotFoundException
*/
public static Bitmap getBitmapFromFile(String filename)
{
try
{
return BitmapFactory.decodeStream(new FileInputStream(Environment
.getExternalStorageDirectory().getPath()
+ "/"
+ filename
+ ".jpg"));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
return null;
}
}
//--------------------------------------------------------------------------------------------------------------------------------------------------
总结一下:
一般来说,第二种和第三种使用方式都可以;因为Intent只能传输数据容量很小的基本数据,当位图很大的时候,很有可能会出现传输错误
比如:FAILED BINDER TRANSACTION;推荐使用第三种方式!!!
package com.example.test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Environment;
public class BitmapUtil
{
/**
* 保存Bitmap为文件;可能报空指针是因为没有配置权限
*
* @param bmp
* @param filename
* @return
*/
public static void saveBitmap2file(Bitmap bmp, String filename)
{
CompressFormat format = Bitmap.CompressFormat.JPEG;
int quality = 100;
OutputStream stream = null;
try
{
stream = new FileOutputStream(Environment
.getExternalStorageDirectory().getPath()
+ "/"
+ filename
+ ".jpg");
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
bmp.compress(format, quality, stream);
try
{
stream.flush();
stream.close();
} catch (IOException e)
{
e.printStackTrace();
}
}
/**
* 读取文件为Bitmap
*
* @param filename
* @return
* @throws FileNotFoundException
*/
public static Bitmap getBitmapFromFile(String filename)
{
try
{
return BitmapFactory.decodeStream(new FileInputStream(Environment
.getExternalStorageDirectory().getPath()
+ "/"
+ filename
+ ".jpg"));
} catch (FileNotFoundException e)
{
e.printStackTrace();
}
return null;
}
}