将位图从一个Activity转移到另一个Activity时无法启动Activity

时间:2012-12-06 06:19:45

标签: android

  

可能重复:
  How to pass image data from one activity to another activity?
  pass a bitmap image from one activity to another

我试图从下面的视图中获取位图

     View s = ml.getRootView();
                s.setDrawingCacheEnabled(true);

            Bitmap  b = s.getDrawingCache();


                System.out.println(b.isRecycled());


                Log.e("ok","ok");
                Intent i=new Intent(CameraActivity.this,Update.class);
                 i.putExtra("data",b);
                 startActivity(i);
//                 s.setDrawingCacheEnabled(false);
                 s.setVisibility(View.GONE);
                 finish();

I tried to transfer the bitmap from this Activity to another,but it is showing me black screen and app gets crashed.
       s.setVisibility(View.GONE); If I dont give this statement it is showing me this modified Activity and then it is showing Black screen.

我认为传输此位图需要花费很多时间。

我该如何解决这个问题?

请帮助

由于

获取黑屏后的Logcat

12-06 11:23:34.654: E/AndroidRuntime(16900): FATAL EXCEPTION: main
12-06 11:23:34.654: E/AndroidRuntime(16900): java.lang.NullPointerException
12-06 11:23:34.654: E/AndroidRuntime(16900):    at android.app.ActivityThread.handleStopActivity(ActivityThread.java:2595)
12-06 11:23:34.654: E/AndroidRuntime(16900):    at android.app.ActivityThread.access$1800(ActivityThread.java:124)
12-06 11:23:34.654: E/AndroidRuntime(16900):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:985)
12-06 11:23:34.654: E/AndroidRuntime(16900):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-06 11:23:34.654: E/AndroidRuntime(16900):    at android.os.Looper.loop(Looper.java:123)
12-06 11:23:34.654: E/AndroidRuntime(16900):    at android.app.ActivityThread.main(ActivityThread.java:3806)
12-06 11:23:34.654: E/AndroidRuntime(16900):    at java.lang.reflect.Method.invokeNative(Native Method)
12-06 11:23:34.654: E/AndroidRuntime(16900):    at java.lang.reflect.Method.invoke(Method.java:507)
12-06 11:23:34.654: E/AndroidRuntime(16900):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-06 11:23:34.654: E/AndroidRuntime(16900):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-06 11:23:34.654: E/AndroidRuntime(16900):    at dalvik.system.NativeStart.main(Native Method)
12-06 11:23:36.490: W/InputManagerService(1318): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40a84730

1 个答案:

答案 0 :(得分:2)

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

使用以下2解决方案来解决此问题。

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 intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

从Bundle获取字节数组并转换为位图图像: -

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。