我试图使用Intent.But将BItmap []从一个活动传递给另一个活动,但它没有被传递,结果导致了一个空指针异常。我用相机拍摄了四张图片并将它们存储在一个Bitmap []中,我试图传递给另一个活动.logcat说错误是在第94行,这是使用传递的Bitmap的地方。这是代码(只有相关部分)。请帮助,提前致谢!
采取Pic.java:
onCreateMethod:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.takepic);
initializeVar();
storeInArray(images);
}
四个imageViews(启动Camera活动)的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;
}
最后,logcat:
12-05 13:34:04.207: W/dalvikvm(3846): threadid=1: thread exiting with uncaught exception (group=0x40e62498)
12-05 13:34:04.207: E/test(3846): Exception
12-05 13:34:04.227: E/AndroidRuntime(3846): FATAL EXCEPTION: main
12-05 13:34:04.227: E/AndroidRuntime(3846): java.lang.NullPointerException
12-05 13:34:04.227: E/AndroidRuntime(3846): at com.example.bethechange.NewEmail$1.onClick(NewEmail.java:94)
12-05 13:34:04.227: E/AndroidRuntime(3846): at android.view.View.performClick(View.java:4106)
12-05 13:34:04.227: E/AndroidRuntime(3846): at android.view.View$PerformClick.run(View.java:17150)
12-05 13:34:04.227: E/AndroidRuntime(3846): at android.os.Handler.handleCallback(Handler.java:615)
12-05 13:34:04.227: E/AndroidRuntime(3846): at android.os.Handler.dispatchMessage(Handler.java:92)
12-05 13:34:04.227: E/AndroidRuntime(3846): at android.os.Looper.loop(Looper.java:137)
12-05 13:34:04.227: E/AndroidRuntime(3846): at android.app.ActivityThread.main(ActivityThread.java:4792)
12-05 13:34:04.227: E/AndroidRuntime(3846): at java.lang.reflect.Method.invokeNative(Native Method)
12-05 13:34:04.227: E/AndroidRuntime(3846): at java.lang.reflect.Method.invoke(Method.java:511)
12-05 13:34:04.227: E/AndroidRuntime(3846): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
12-05 13:34:04.227: E/AndroidRuntime(3846): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
12-05 13:34:04.227: E/AndroidRuntime(3846): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
不要通过intent将位图数组传递给另一个活动。 位图可能是大文件。它将导致“TransactionTooLargeException”
您有2种方法可以解决此问题:
1:使用全局单例(如果位图是文件中不存在的临时数据)
2:传递位图文件路径
答案 1 :(得分:0)
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent,CAMERA_REQUEST);;
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
}
}
}