ImageView没有显示捕获的照片android

时间:2014-08-30 16:29:34

标签: android camera imageview

我是android开发的新手。我一直试图访问相机,拍摄照片,然后返回我的活动并在ImageView上显示它。

以下是我的活动代码(非常简单,只是这样做)

public class Profile extends Activity  {

    private static final int CAMERA_REQUEST = 1337;
    private ImageView imagePhoto;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_profile);
        imagePhoto = (ImageView) findViewById(R.id.imagePhoto);
        ImageView photoButton = (ImageView) this.findViewById(R.id.camera);
        photoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                startActivityForResult(cameraIntent, CAMERA_REQUEST); 
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if( requestCode == CAMERA_REQUEST)
        {
            Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
            ImageView image =(ImageView) findViewById(R.id.imagePhoto);
            image.setImageBitmap(thumbnail);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

我做错了什么?我搜索并尝试了这么多代码。当我点击按钮时,相机显示,我拍照,然后我返回活动,但照片的缩略图没有显示在ImageView" imagePhoto"。

以下是imagePhoto的布局:

 <ImageView
         android:id="@+id/imagePhoto"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_below="@+id/welcome"
         android:layout_marginRight="50dp"
         android:layout_marginTop="82dp"
         android:layout_toLeftOf="@+id/camera"
         android:src="@drawable/add_photo" /> `/* It has another photo, because I was trying to see if it changes */

这也在我的清单中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

我尝试了很多代码!提前致谢。我使用的是三星设备。

1 个答案:

答案 0 :(得分:1)

您的代码看起来很好,但我想分享我的,因为它适合我

此代码还会在内存中创建一个文件夹并将图像保存在那里

这是代码

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {
   ImageView image;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     image =(ImageView) findViewById(R.id.imagePhoto);
        // create external folder
        createFolder();
          Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
          startActivityForResult(cameraIntent, 1888); 

    }
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
            if (requestCode == 1888 && resultCode == RESULT_OK) {  


         Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            image.setImageBitmap(photo );

                    // to create a random image file name
                    Random generator = new Random();
                    int n = 10000;
                    n = generator.nextInt(n);
                    String fname = "Image-"+ n +".jpg";

                String filePath = "/sdcard/PictureFolder/"+fname;
                FileOutputStream fileOutputStream = null;
                try {
                    fileOutputStream = new FileOutputStream(filePath);
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                BufferedOutputStream bos = new BufferedOutputStream(fileOutputStream);

                //choose another format if PNG doesn't suit you
                photo.compress(CompressFormat.PNG, 100, bos);

                try {
                    bos.flush();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }  
        } 

      public void createFolder()
      {
          String RootDir = Environment.getExternalStorageDirectory()
                  + File.separator + "PictureFolder";
          File RootFile = new File(RootDir);
          RootFile.mkdir();
      }
}

只有我添加此权限

 <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />