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

一年好景君须记,最是橙黄橘绿时。这篇文章主要讲述在android [copy]中没有设置从相机到imageview的图像相关的知识,希望能为你提供帮助。
这个问题在这里已有答案:

  • Capture Image from Camera and Display in Activity 12个答案
我试图显示从camera内部imageview捕获的图像在android中选择正确的图库中的照片工作但相机失败:“应用已停止”
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) {//galleryIntent 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(); }

答案首先确保您在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); } } }

并且不要忘记将qazxsw poi添加到qazxsw poi-
permission

另一答案
manifest.xml

然后将字节转换为位图
< uses-feature android:name="android.hardware.camera" android:required="true" /> < uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
【在android [copy]中没有设置从相机到imageview的图像】然后设置imageview new Camera.PictureCallback() { @Override public void onPictureTaken(final byte[] data, Camera camera) { bytes = data; } }

    推荐阅读