【问题标题】:Display photo in GridView taken from camera在 GridView 中显示从相机拍摄的照片
【发布时间】:2015-09-15 05:01:17
【问题描述】:

这是我将照片从一项活动发送到另一项活动的代码! 我启动相机并在确定时照片显示在第二个活动中。 现在,我希望将照片显示在 GridView 中。 需要注意的一点: 1. 在相机活动中,我存储到 File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages"); 但不明白为什么每次拍照都会被替换用新的。

CameraActivity:

 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;
    }
}
}

接收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>

【问题讨论】:

  • 您对新捕获的图像使用相同的名称,因此新图像将替换为前一个尝试按照@bpAFree 建议将当前时间附加到图像名称之后。

标签: android gridview


【解决方案1】:

但不明白为什么每次我拍照都会被替换为 新的。

因为你使用File image = new File(imagesFolder, "image_001.jpg");

这张照片只有一个名称,即 image_001 ,请添加一些独特的东西作为

File image = new File(imagesFolder,"image_001"+System.currentTimeMillis()+".jpg");

因为每次都会制作新图像。

【讨论】:

  • 酷,解决了重复部分。现在问题是我想在 GridView 中显示它们我似乎无法理解代码有什么问题!
  • 在网格视图中显示图像有什么问题
  • 所以当我使用上面的代码时,我在 ReceivingActivity 中没有得到图像。
  • 你什么时候使用适配器/gridview?链接该代码..我没有看到在任何地方创建适配器(应该调用一个方法来实例化适配器/从xml设置适配器到GridView)
  • 在 ReceivingActivity 检查中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-11-17
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多