这是我将照片从一个活动发送到另一个活动的代码! 我启动相机,然后在第二个活动中显示照片。 现在,我希望照片显示在GridView中。 需要注意的一点: 1.在相机活动中,我存储到文件imagesFolder =新文件(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();
}
}
ReceivingActivity:
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;
}
}
}
ReceivingXML:
<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>
答案 0 :(得分:0)
但无法理解为什么每次拍照都会被替换 新的。
因为您使用File image = new File(imagesFolder, "image_001.jpg");
这个拍摄图片只有一个名字,即image_001,请添加一些独特的东西作为
File image = new File(imagesFolder,"image_001"+System.currentTimeMillis()+".jpg");
每次制作新图片。