在android中没有设置从相机到imageview的图像

时间:2018-01-02 06:07:43

标签: android imageview android-camera-intent

我正在尝试显示从android camera内的imageview中捕获的图像 从正确的图库中选择照片有效 但相机失败了:“应用程序停止了”

images.get(2).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    current_image = 2;
                    if (!fill_images[current_image]) {
                        show_dialog();
                    }else {
                        show_delete_dialog();
                    };
                }
            });

 public void show_dialog() {        
                     final ArrayList<String> list = new ArrayList<String>();
                     list.add("select from gallery");
                     list.add("take a photo");

                     AlertDialog.Builder builder = new AlertDialog.Builder(NewAdActivity.this);

                     builder.setAdapter(new ArrayAdapter<String>(NewAdActivity.this, R.layout.row, R.id.mytext, list)
                             , new DialogInterface.OnClickListener() {
                                 @Override
                                 public void onClick(DialogInterface dialogInterface, int i) {

            if (i == 0) {//gallery                           Intent gallery_intent=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;               startActivityForResult(Intent.createChooser(gallery_intent,"select image"),2);

                                     }
 else if (i == 1){//camera
          Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

      File file=new File(Environment.getExternalStorageDirectory(),"file"+String.valueOf(System.currentTimeMillis()+".jpg"));

              uri= Uri.fromFile(file);

            camera_intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);

             camera_intent.putExtra("return-data",true);

         startActivityForResult(camera_intent,1); 

                                     }
                                 }
                             });
                     builder.show();

                 }

2 个答案:

答案 0 :(得分:0)

  new Camera.PictureCallback(){&#xA; @越权#XA; public void onPictureTaken(final byte [] data,Camera camera){&#xA; bytes = data;&#xA; }&#xA;}&#xA;  
&#xA;&#xA;

然后将字节转换为位图

&#xA;&#xA;

Bitmap bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);

&#xA;&#xA;

然后设置imageview&#xA; imageView.setImageBitmap (位图);

&#XA;

答案 1 :(得分:0)

首先确保您在 manifest.xml 文件中声明了所有必要的权限。然后尝试一下 -

public class MainActivity extends AppCompatActivity {

    private static final int SELECTED_PICTURE=1;
    private ImageView iv;
    private Button btnClick;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        iv=(ImageView)findViewById(R.id.imageView1);
        btnClick = (Button) findViewById(R.id.btnClick);
        btnClick.setOnClickListener(new View.OnCLickListener(){
              public void onClick(View view) {
            btnClick(view);
        }
        });
    }

    public void btnClick(View v){
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, SELECTED_PICTURE);
    }

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        iv.setImageBitmap(photo);
    }  
    }
}

不要忘记将permission添加到manifest.xml -

<uses-feature
        android:name="android.hardware.camera"
        android:required="true" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />