Android 在GridView中显示从相机拍摄的照片
这是我将照片从一个活动发送到另一个活动的代码! 我启动相机,并在OK时,在第二个活动中显示photo get。 现在,我希望照片显示在GridView中。 需要注意的一点是: 1.在照相机活动中,我将存储到文件imagesFolder=new文件(Environment.getExternalStorageDirectory(),“MyImages”)但我不明白为什么每次我拍照都会换成新的 摄像活动:Android 在GridView中显示从相机拍摄的照片,android,gridview,Android,Gridview,这是我将照片从一个活动发送到另一个活动的代码! 我启动相机,并在OK时,在第二个活动中显示photo get。 现在,我希望照片显示在GridView中。 需要注意的一点是: 1.在照相机活动中,我将存储到文件imagesFolder=new文件(Environment.getExternalStorageDirectory(),“MyImages”)但我不明白为什么每次我拍照都会换成新的 摄像活动: button1.setOnClickListener(new View.OnClickLis
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(!imageTaken){
Toast.makeText(getActivity(), "You've not taken any image!", Toast.LENGTH_SHORT).show();
return;
}
Intent iSecond=new Intent(getActivity(),ShowImage.class);
iSecond.putExtra("pictureUri", imageUri.toString());
startActivity(iSecond);
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getActivity(), "Launching Camera", Toast.LENGTH_SHORT).show();
Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
if(imagesFolder.mkdirs()){
Log.d(TAG, "The directory is created");
} else {
Log.d(TAG, "Failed or already exists");
}
File image = new File(imagesFolder, "image_001.jpg");
try {
if(image.createNewFile()){
Log.d(TAG, "The file is created");
} else {
Log.d(TAG, "The file already exists");
}
} catch (IOException e) {
Log.e(TAG, "createNewFile Failed");
e.printStackTrace();
}
imageUri = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(imageIntent, REQUEST_CODE_FROM_CAMERA);
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE_FROM_CAMERA && resultCode == Activity.RESULT_OK) {
imageTaken = true;
Toast.makeText(getActivity(), "The image was taken",Toast.LENGTH_SHORT).show();
}
}
接收活动:
public static final String GridViewDemo_ImagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyImages/";
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
Uri thePic =(Uri.parse(getIntent().getStringExtra("pictureUri")));
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), thePic);
} catch (IOException e) {
e.printStackTrace();
}
}
public class ImageListAdapter extends BaseAdapter
{
private Context context;
private List<String> imgPic;
public ImageListAdapter(Context c, List<String> bitmap)
{
context = c;
imgPic = bitmap;
}
public int getCount() {
if(imgPic != null)
return imgPic.size();
else
return 0;
}
//---returns the ID of an item---
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
//---returns an ImageView view---
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imageView;
BitmapFactory.Options bfOptions=new BitmapFactory.Options();
bfOptions.inDither=false; //Disable Dithering mode
bfOptions.inPurgeable=true; //Tell to gc that whether it needs free memory, the Bitmap can be cleared
bfOptions.inInputShareable=true; //Which kind of reference will be used to recover the Bitmap data after being clear, when it will be used in the future
bfOptions.inTempStorage=new byte[32 * 1024];
if (convertView == null) {
imageView = new ImageView(context);
imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
FileInputStream fs = null;
Bitmap bm;
try {
fs = new FileInputStream(new File(imgPic.get(position).toString()));
if(fs!=null) {
bm= BitmapFactory.decodeFileDescriptor(fs.getFD(), null, bfOptions);
imageView.setImageBitmap(bm);
imageView.setId(position);
imageView.setLayoutParams(new GridView.LayoutParams(200, 160));
}
} catch (IOException e) {
e.printStackTrace();
} finally{
if(fs!=null) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return imageView;
}
}
}
public静态最终字符串GridViewDemo_ImagePath=Environment.getExternalStorageDirectory().getAbsolutePath()+“/MyImages/”;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.show);
Uri thePic=(Uri.parse(getIntent().getStringExtra(“pictureUri”));
试一试{
位图Bitmap=MediaStore.Images.Media.getBitmap(this.getContentResolver(),thePic);
}捕获(IOE异常){
e、 printStackTrace();
}
}
公共类ImageListAdapter扩展了BaseAdapter
{
私人语境;
私有列表imgPic;
公共ImageListAdapter(上下文c,列表位图)
{
上下文=c;
imgPic=位图;
}
public int getCount(){
如果(imgPic!=null)
返回imgPic.size();
其他的
返回0;
}
//---返回项目的ID---
公共对象getItem(int位置){
返回位置;
}
公共长getItemId(int位置){
返回位置;
}
//---返回ImageView视图---
公共视图getView(int位置、视图转换视图、视图组父视图)
{
图像视图图像视图;
BitmapFactory.Options bfOptions=新的BitmapFactory.Options();
bfOptions.inDither=false;//禁用抖动模式
bfOptions.inpurgable=true;//告诉gc是否需要空闲内存,位图可以被清除
bfOptions.inInputShareable=true;//清除位图数据后,将使用哪种引用来恢复位图数据,以及将来何时使用
bfOptions.inTempStorage=新字节[32*1024];
if(convertView==null){
imageView=新的imageView(上下文);
imageView.setLayoutParams(新的GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_父级,ViewGroup.LayoutParams.MATCH_父级));
setPadding(0,0,0,0);
}否则{
imageView=(imageView)convertView;
}
FileInputStream fs=null;
位图bm;
试一试{
fs=newfileinputstream(新文件(imgPic.get(position.toString()));
如果(fs!=null){
bm=BitmapFactory.decodeFileDescriptor(fs.getFD(),null,bfOptions);
设置图像位图(bm);
imageView.setId(位置);
setLayoutParams(新的GridView.LayoutParams(200160));
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
如果(fs!=null){
试一试{
fs.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
返回图像视图;
}
}
}
接收XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<RelativeLayout
android:id="@+id/RelativeGridLayout"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
<GridView
android:id="@+id/gridviewimg"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center"
android:numColumns="2"
android:scrollbarStyle="outsideInset"
android:smoothScrollbar="true"
android:verticalSpacing="10dp"
android:paddingBottom="50dp"
android:paddingTop="10dp"
/>
</RelativeLayout>
<RelativeLayout
android:id="@+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignBottom="@+id/RelativeGridLayout"
>
</RelativeLayout>
但我不明白为什么每次我拍照都会被替换为
新的
因为您使用File image=new文件(imagesFolder,“image_001.jpg”)代码>
此拍摄图像只有一个名称,即图像_001,请添加一些唯一的名称
File image=新文件(imagesFolder,“image_001”+System.currentTimeMillis()+“.jpg”)代码>
因为每次这样做都会产生新的图像。很酷,这就解决了重复部分的问题。现在的问题是,我想在GridView中显示它们,我似乎不明白代码有什么问题!在grid View中显示图像有什么问题?因此,当我使用上述代码时,在ReceivingActivity中没有图像。您何时使用适配器/gridview执行任何操作?链接该代码。。我看不到适配器在任何地方被创建(应该调用一个方法将适配器/设置适配器从xml实例化到GridView),它在ReceivingActivity检查中。您对新捕获的图像使用了相同的名称,因此新图像将替换为前一个图像。请尝试按照@bpAFree建议将当前时间附加到图像名称之后。