在android [copy]中没有设置从相机到imageview的图像
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在android [copy]中没有设置从相机到imageview的图像相关的知识,希望对你有一定的参考价值。
这个问题在这里已有答案:
我试图显示从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) {//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();
}
答案
首先确保您在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" />
然后设置imageview new Camera.PictureCallback() {
@Override
public void onPictureTaken(final byte[] data, Camera camera) {
bytes = data;
}
}
以上是关于在android [copy]中没有设置从相机到imageview的图像的主要内容,如果未能解决你的问题,请参考以下文章
Android:从设置外部更改相机权限会导致应用程序共享首选项清除