Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我无法将相机拍摄的图像从android中的一个活动传输到另一个活动?_Android_File_Camera - Fatal编程技术网

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

我无法将相机拍摄的图像从android中的一个活动传输到另一个活动?,android,file,camera,Android,File,Camera,我知道如何将从相机拍摄的图像从一个活动传输到另一个活动。但在第二个活动中,图像不会显示。我知道这很简单。但我被困在这里。请告诉我我哪里出错了。我正在通过字节数组传输图像,位图上出现空指针异常已被转移 我的第一项活动是 import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java

我知道如何将从相机拍摄的图像从一个活动传输到另一个活动。但在第二个活动中,图像不会显示。我知道这很简单。但我被困在这里。请告诉我我哪里出错了。我正在通过字节数组传输图像,位图上出现空指针异常已被转移

我的第一项活动是

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);
    }
  }
  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 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);
    }
  }
  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);
      }
  }
 }
 }

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

试试这个答案
 File file = new File("your sdcard path");
    Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
在第一个活动中

    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);

在这里,您可以选择让它通过sd卡的直接路径,在那里您的图像被捕获

将字节数组从一个活动传递到另一个活动是不好的做法

我认为你应该解码sd卡的路径,然后再试一次。应该是工作

1.)首先以字符串形式在第二个活动中传递路径

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

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

我只是编辑了你的代码,希望这对你有帮助

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);
            }
        }

    }

}
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);
    }
}
Temp.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);
            }
        }

    }

}
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);
    }
}
替换intent.putExtra(MediaStore.EXTRA_输出,Uri.fromFile(f));这条线符合 intent.putExtra(“image”,f.getAbsolutePath())

并在第二个活动中使用 getintent().getextras.getString(“图像”)


就这样。。。我希望这能帮助你……

我已经找到了答案。我在这里写下我的两个活动

我的第一项活动是

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);
    }
  }
  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 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>

"

看看我的ans,我在这里尝试了这种方式。我没有收到任何空指针异常。但是图像没有显示在第二个活动中。你能告诉我你传递路径的方式吗图像没有显示在第二个活动中有你可以得到的图像路径只能从路径解码然后你可以将其设置为imageviewString imagePath=getIntent().getStringExtra(“图像”);位图位图=BitmapFactory.decodeFile(imagePath);iv.setImageBitmap(位图);我已经尝试了你的代码。第二个活动中没有显示图像。只有空白屏幕。你能建议其他方式吗?应用程序崩溃了吗?你的设备中有哪个版本的Android?我有3台设备。两台带有姜黄鸟,一台带有冰淇淋三明治。这里的图像应该从相机中拍摄。我知道你的代码适用于位图wh非物质文化遗产是由资源形成的