我在弄清楚如何拍摄相机拍摄的图像并将其显示在应用程序的图库中时遇到问题。在我的mainActivity中,我使用: 公共类MainActivity扩展了AppCompatActivity {
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton Camera;
Camera= findViewById(R.id.CameraID);
Camera.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent Intent3=new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivity(Intent3);
}
});
}
这会启动相机。拍照后,照片将存储到手机中的SD卡中。
我有一个带图像数组的图库类。这是我要显示图片的地方。我意识到使用R.id.name格式没有意义,因为这些图像不在我的可绘制文件夹中,但是我需要用一些东西填充它,以便可以测试Gallery.Layout.xml页面。我的画廊课如下: 公共类Gallery扩展AppCompatActivity { 私人Integer []图片= {R.drawable.fish1,R.drawable.fish2,R.drawable.fish3, R.drawable.fish4,R.drawable.fish5,R.drawable.fish6,R.drawable.fish7,R.drawable.fish8}; 私人ImageView imageview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallary);
Intent intent = getIntent();
android.widget.Gallery imgGallery = (android.widget.Gallery) findViewById(R.id.gallery);
imgGallery.setAdapter(new ImageAdapter(this));
imageview = (ImageView) findViewById(R.id.imageView);
imgGallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Toast.makeText(getApplicationContext(), "Image " + arg2,Toast.LENGTH_SHORT).show();
imageview.setImageResource(images[arg2]);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context context;
int imageBackground;
public ImageAdapter(Context context) {
this.context = context;
}
@Override
public int getCount() {
return images.length;
}
@Override
public Object getItem(int arg0) {
return arg0;
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(images[arg0]);
return imageView;
}
}
}