Java 通过Intent传递位图时出现空指针异常
我试图使用Intent将位图[]从一个活动传递到另一个活动。但是,它没有被传递,我想这会导致空指针异常。我用相机拍摄了多达四幅图像,并将它们存储在位图[]中,我正试图将其传递给另一个活动。logcat说错误出现在第94行,这是使用传递的位图的位置。以下是代码,仅适用于相关部分。请帮忙,提前谢谢 以Pic.java为例: onCreate方法:Java 通过Intent传递位图时出现空指针异常,java,android,android-intent,bitmap,Java,Android,Android Intent,Bitmap,我试图使用Intent将位图[]从一个活动传递到另一个活动。但是,它没有被传递,我想这会导致空指针异常。我用相机拍摄了多达四幅图像,并将它们存储在位图[]中,我正试图将其传递给另一个活动。logcat说错误出现在第94行,这是使用传递的位图的位置。以下是代码,仅适用于相关部分。请帮忙,提前谢谢 以Pic.java为例: onCreate方法: @Override public void onCreate(Bundle savedInstanceState) { supe
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.takepic);
initializeVar();
storeInArray(images);
}
启动相机活动的四个ImageView的onActivity结果:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1 && resultCode == RESULT_OK) {
photo1 = (Bitmap) data.getExtras().get("data");
imageView1.setImageBitmap(photo1);
} if(requestCode == 2 && resultCode == RESULT_OK) {
photo2 = (Bitmap) data.getExtras().get("data");
imageView2.setImageBitmap(photo2);
} if(requestCode == 3 && resultCode == RESULT_OK) {
photo3 = (Bitmap) data.getExtras().get("data");
imageView3.setImageBitmap(photo3);
} if(requestCode == 4 && resultCode == RESULT_OK) {
photo4 = (Bitmap) data.getExtras().get("data");
imageView4.setImageBitmap(photo4);
}
}
在数组中存储单个位图的方法:
private Bitmap[] storeInArray(Bitmap[] bitmap) {
// TODO Auto-generated method stub
if(photo1 != null){
bitmap[i]= photo1;
i++;
}if(photo2 != null){
bitmap[i]= photo2;
i++;
}if(photo3 != null){
bitmap[i]= photo3;
i++;
}if(photo4 != null){
bitmap[i]= photo4;
i++;
}
return bitmap;
}
最后,按钮的onClickListener:
bCamEmail.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(TakePic.this,NewEmail.class);
i.putExtra("Image",images);
startActivity(i);
}
});
在另一类中:
extras = getIntent().getExtras();
if(extras!=null)
receive = (Bitmap[]) extras.getParcelableArray("Image");
对于“发送电子邮件”按钮:
newSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
convertEditTextToString();
Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
emailIntent.setType("text/plain");
emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,"seemaswain.09@gmail.com");
emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,newSubject);
emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,newContent);
newUris = new ArrayList<Uri>();
for(String file : images) {
File fileIn = new File(file);
Uri u = Uri.fromFile(fileIn);
newUris.add(u);
}
emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, newUris);
startActivity(emailIntent);
}
});
}
public String[] BitMapToString(Bitmap bitmap[]){
int i=0;
String[] temp= new String[2000];
while(bitmap[i] !=null){
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap[i].compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
temp[i]=Base64.encodeToString(b, Base64.DEFAULT);
i++;
}
return temp;
}
不要故意将位图数组传递给其他活动。 位图可能是大文件。它将导致TransactionTooLargeException 有两种方法可以解决此问题: 1:如果位图是文件中不存在的临时数据,请使用全局单音
2:传递位图文件路径Intent-cameraIntent=new-Intentandroid.provider.MediaStore.ACTION\u-IMAGE\u-CAPTURE; startActivityForResultcameraIntent,摄像头请求
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
try {
photo = (Bitmap) data.getExtras().get("data");
} catch (Exception e) {
// TODO: handle exception
}
}
}
不建议有意保留图像。最好将图像保存在一个文件中,并将该文件的路径发送到下一个类。这是哪一行?@sayed.jalil这是字符串文件:images{..this line..receive=Bitmap[]extras.getParcelableArrayImage;您将图像接收到receive中,但正在对图像进行迭代。确定吗?您稍后已将接收分配给图像?确保已初始化位图[]使用正确的大小!我无法理解第一种方法。:/使用全局单例对象,然后可以将位图数组放入该对象中,并通过全局对象访问它。例如:DataCenter.getSingleton.getMyBitmaps
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
try {
photo = (Bitmap) data.getExtras().get("data");
} catch (Exception e) {
// TODO: handle exception
}
}
}