我无法将从摄像头拍摄的图像从一个活动传输到另一个活动在android中?

时间:2014-05-14 05:44:45

标签: android file camera

我知道如何将从相机拍摄的图像从一个活动转移到另一个活动。但是这里的图像没有显示在第二个活动中。我知道这很容易。但我被困在这里。请告诉我在哪里我犯了错误。我正在通过字节数组传输图像,并且我在传输的位图上得到空指针异常。

我的第一项活动是

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

 public class Working extends Activity{
ImageView first,second;
Button set;
Bitmap bitmap,scaled;
RelativeLayout relative;
String path;
File file;

protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.working);
 first=(ImageView)findViewById(R.id.imageView1);
 set=(Button)findViewById(R.id.button1);                 
 set.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

    startActivityForResult(intent, 1);
}
 });

 }

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK) {

            if (requestCode == 1) {

                File f = new File(Environment.getExternalStorageDirectory().toString());

                for (File temp : f.listFiles()) {

                    if (temp.getName().equals("temp.jpg")) {

                        f = temp;

                        break;

                    }

                }

                try {

                    Bitmap bitmap;

                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    bitmapOptions.inSampleSize=2;

                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),

                            bitmapOptions); 



                    first.setImageBitmap(bitmap);



                     path = android.os.Environment .getExternalStorageDirectory() + File.separator+ "Phoenix" + File.separator + "default";

                    f.delete();

                    OutputStream outFile = null;

                    file = new File(path, String.valueOf(System.currentTimeMillis()) + ".png");

                    try {

                        outFile = new FileOutputStream(file);

                        bitmap.compress(Bitmap.CompressFormat.PNG, 100, outFile);

                        outFile.flush();

                        outFile.close();

                    } catch (FileNotFoundException e) {

                        e.printStackTrace();

                    } catch (IOException e) {

                        e.printStackTrace();

                    } catch (Exception e) {

                        e.printStackTrace();

                    }

                } catch (Exception e) {

                    e.printStackTrace();

                }               
                 ByteArrayOutputStream stream = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                Intent intent = new Intent(Working.this, Temp.class);
                intent.putExtra("image", byteArray);
                startActivity(intent);

            }
      }
       }

           }

我的第二项活动是

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;

  public class Temp extends Activity{
   ImageView iv;
protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.temp);
    iv=(ImageView)findViewById(R.id.imageView2);
        Bundle extras = getIntent().getExtras();
    byte[] byteArray = extras.getByteArray("image");

    Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
    iv.setImageBitmap(bitmap);
    }
  }

6 个答案:

答案 0 :(得分:0)

您不应该在intent中传递字节数组,因为如果图像大小很大,它可能会导致问题。相反,为什么不通过意图中的路径并在下一个活动中读取路径。

答案 1 :(得分:0)

试试这个答案 在第一个活动中

    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(firstActivty.this, secondActivity.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 :(得分:0)

在这里你可以选择通过SD卡的直接路径来捕获你的图像。

将Byte数组从一个活动传递到另一个活动并不是一个好习惯。

我认为你应该解码SD卡的路径并在尝试之后。它应该是有效的。

1。)首先在string中的第二个活动中传递路径。

2。)使用密钥获取该字符串,并在第二个活动中应用此逻辑。

 File file = new File("your sdcard path");
    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());

答案 3 :(得分:0)

我只是编辑你的代码,试试希望这可以帮助你

<强> Working.java

public class Working extends Activity {
    ImageView first, second;
    Button set;
    Bitmap bitmap, scaled;
    RelativeLayout relative;
    final private int CAPTURE_IMAGE = 2;
    private String imgPath = "";

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.working);
        first = (ImageView) findViewById(R.id.imageView1);
        set = (Button) findViewById(R.id.button1);
        set.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                startActivityForResult(intent, CAPTURE_IMAGE);
            }
        });

    }

    public Uri setImageUri() {
        // Store image in dcim
        File file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + System.currentTimeMillis() + ".png");
        Uri imgUri = Uri.fromFile(file);
        this.imgPath = file.getAbsolutePath();
        return imgUri;
    }

    public String getImagePath() {
        return imgPath;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == CAPTURE_IMAGE) {
                Intent intent = new Intent(Working.this, Temp.class);
                intent.putExtra("image", getImagePath());
                startActivity(intent);
            } else {
                super.onActivityResult(requestCode, resultCode, data);
            }
        }

    }

}

<强> Temp.java

public class Temp extends Activity {
    ImageView iv;

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        setContentView(R.layout.temp);
        iv = (ImageView) findViewById(R.id.imageView2);

        String imagePath = getIntent().getStringExtra("image");

        Bitmap bitmap = BitmapFactory.decodeFile(imagePath);
        iv.setImageBitmap(bitmap);
    }
}

答案 4 :(得分:0)

替换intent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(f));这一行 intent.putExtra(&#34; image&#34;,f.getAbsolutePath());

并在第二个活动中使用该路径 。getintent()getextras.getString(&#34;图像&#34);

多数民众赞成......我希望这会对你有帮助......

答案 5 :(得分:0)

我找到了答案。我在这里写了两个活动。

我的第一项活动是

  import java.io.File;
  import android.app.Activity;
  import android.content.Intent;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.net.Uri;
  import android.os.Bundle;
  import android.os.Environment;
  import android.provider.MediaStore;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ImageView;
  import android.widget.RelativeLayout;

  public class Working extends Activity{
ImageView first,second;
Button set;
Bitmap bitmap,scaled;
RelativeLayout relative;
String path;
File file;

protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.working);
 first=(ImageView)findViewById(R.id.imageView1);
 set=(Button)findViewById(R.id.button1);                 
  set.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

      File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");

    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));

    startActivityForResult(intent, 1);
}
  });

   }

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if (resultCode == RESULT_OK) {

            if (requestCode == 1) {

                File f = new File(Environment.getExternalStorageDirectory().toString());

                for (File temp : f.listFiles()) {

                    if (temp.getName().equals("temp.jpg")) {

                        f = temp;

                        break;

                    }

                }



                    Bitmap bitmap;

                    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

                    bitmapOptions.inSampleSize=2;

                    bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),

                            bitmapOptions); 



                    first.setImageBitmap(bitmap);
                    Intent i = new Intent(Working.this, Temp.class);
                    i.putExtra("imagePath" ,f.getAbsolutePath() ); 
                    startActivity(i);
      }
  }
 }
 }

我的第二项活动是

import android.app.Activity;
import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.os.Bundle;
 import android.widget.ImageView;

 public class Temp extends Activity{
 ImageView iv;
protected void onCreate(Bundle paramBundle)
  {
    super.onCreate(paramBundle);
    setContentView(R.layout.temp);
    iv=(ImageView)findViewById(R.id.imageView1);
    BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();

    bitmapOptions.inSampleSize=6;    
    String imagePath = getIntent().getStringExtra("imagePath");

    Bitmap bitmap = BitmapFactory.decodeFile(imagePath,bitmapOptions);
    iv.setImageBitmap(bitmap);
 }
 }

我的第一个活动xml文件和第二个活动xml文件是相同的.xml文件是

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/relative" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Button" />

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_alignParentBottom="true"
    android:text="Button" />

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_below="@+id/button1" >

    <ImageView
    android:id="@+id/imageView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:src="@drawable/ic_launcher"/>"

</RelativeLayout>

</RelativeLayout>