无法将图像从一个活动发送到另一个活动

时间:2013-12-26 13:40:50

标签: android android-intent

我正在尝试将相机拍摄的图像从一个活动发送到另一个活动。我通过以下两种方法(来自stackoverflow)尝试了它

方法1:

通过路径传递图片:

String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "Pictures" + File.separator + "SanPics2";
Intent image_intent = new Intent(Snap_ImageView.this,FullScreen.class);
                        image_intent.putExtra("image_path" ,path );
                        System.out.println("Inent has passed the extras");
                        startActivity(image_intent);

在接收结束时(FullScreen.java):

final String path = getIntent().getStringExtra("image_Path");
Bitmap org_bmp = BitmapFactory.decodeFile(path);
imageview.setImageBitmap(org_bitmap);

此方法似乎没有设置图像,但它到达目标活动并显示纯白色背景,很奇怪!!方法1的Logcat如下:

方法1 Logcat:

12-26 18:57:01.520: D/dalvikvm(6137): GC_FOR_ALLOC freed 897K, 35% free 9156K/13955K, paused 221ms, total 221ms
12-26 18:57:01.970: D/dalvikvm(6137): GC_CONCURRENT freed 88K, 11% free 40280K/45191K, paused 28ms+28ms, total 96ms
12-26 18:57:02.790: I/System.out(6137): /storage/sdcard0/Pictures/SanPics2
12-26 18:57:06.270: I/System.out(6137): Inent has passed the extra
12-26 18:57:06.280: I/Choreographer(6137): Skipped 302 frames!  The application may be doing too much work on its main thread.

不会报告任何问题,但同时不会设置预期的图像。

方法2:

通过ByteArray传递图片:

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

在接收结束时(FullScreen.java):

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 Logcat:

12-26 19:04:12.620: D/dalvikvm(7237): GC_FOR_ALLOC freed 1322K, 34% free 9244K/13955K, paused 144ms, total 169ms
12-26 19:04:12.860: D/dalvikvm(7237): GC_CONCURRENT freed 90K, 11% free 40366K/45191K, paused 25ms+14ms, total 73ms
12-26 19:04:14.350: I/System.out(7237): /storage/sdcard0/Pictures/SanPics2
12-26 19:04:19.850: I/System.out(7237): Inent has passed the extra
12-26 19:04:19.930: I/Choreographer(7237): Skipped 454 frames!  The application may be doing too much work on its main thread.

这与方法1 Logcat相同。我完全迷失在这里,因为这两种方法都不起作用,我无法找到内部发生的事情。任何想法都将受到高度赞赏。 如果其中任何一项活动可以发挥作用,那就太棒了。

注意:

我在下面描述的代码中调用了两种意图风格:

Bitmap bitmap;
                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
                            bitmapOptions);
                    view_image.setImageBitmap(bitmap);
                    String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "Pictures" + File.separator + "SanPics2";
                    System.out.println(path);
                    f.delete();
                    OutputStream outFile = null;
                    File file = new File(path, String.valueOf(System
                            .currentTimeMillis()) + ".jpg");

                    try {
                        outFile = new FileOutputStream(file);
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
                        ByteArrayOutputStream byte_array_stream = new ByteArrayOutputStream();
                        bitmap.compress(Bitmap.CompressFormat.JPEG, 85, byte_array_stream);
                        byte[] byte_array = byte_array_stream.toByteArray();
                        ****Intent image_intent = new Intent(Snap_ImageView.this,FullScreen.class);
                        image_intent.putExtra("image_path" ,byte_array );//Similarly path intent style at the same position
                        System.out.println("Inent has passed the extra");
                        startActivity(image_intent);****
                        outFile.flush();
                        outFile.close();

                    }

这是否与目的地活动中图像显示的限制有关?我只是一个新鲜的,所以我道歉,如果这些问题听起来很愚蠢。干杯!!

2 个答案:

答案 0 :(得分:1)

所以你想要什么?你已经保存了照片并需要从其他活动中打开它吗?

将它放入缩进并发送

filepath = "Your Image Path"
intent.putExtra("imagePath", filepath); 

从另一项活动中接收

String image_path = getIntent().getStringExtra("imagePath");
Bitmap bitmap = BitmapFactory.decodeFile(image_path);
myimageview.setImageDrawable(bitmap);

如果我弄错了,请告诉我!

<强>编辑:

当你需要时,从Bitmap转换Drawable

Bitmap image = BitmapFactory.decodeResource(context.getResources(), R.drawable.cam_image);

或试试这个

Bitmap bitmap = ((BitmapDrawable)ur_drawable).getBitmap();

答案 1 :(得分:0)

问题是我没有给出图像扩展名和文件名。这就是我遇到的问题。如果添加以下内容,方法1效果很好:

String path = android.os.Environment
                            .getExternalStorageDirectory()
                            + File.separator
                            + "Pictures" + File.separator + "SanPics2";
File file = new File(path,"Your image name.jpg");//**Extension is what matters**
String image_name = file.toString();
Intent image_intent = new Intent(Snap_ImageView.this,FullScreen.class);
                        image_intent.putExtra("image_path" ,image_name );
                        System.out.println("Inent has passed the extras");
                        startActivity(image_intent);

这对我有用。