gpt4 book ai didi

java - 从相机捕获图像并设置到android中的 ListView 中

转载 作者:太空宇宙 更新时间:2023-11-04 14:16:26 24 4
gpt4 key购买 nike

我在做什么::

  • 我正在通过单击操作栏菜单中的项目来打开相机
  • 我正在捕获图像并将其设置在 ListView 中

发生了什么::

  • 假设我捕获了 10 张图像并将其设置在 ListView 中
  • 下次运行我的代码时,我能够找到我上次拍摄的图像时间,而且不是从头开始

我正在尝试做什么:

  • 假设我拍摄了 10 张图像并设置在 ListView 中
  • 下次我启动应用程序并开始捕获它应该添加的图像时新捕获的图像到 ListView 而不显示旧图像
  • 我并不是告诉我必须删除这些图像,而是我想要的应用程序每次都显示新捕获的图像
<小时/>

MainActivity.java

public class MainActivity extends ListActivity {

private static final int CAMERA_CAPTURE = 20;
ArrayList<String> listOfImages;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DisplayCapturedImagesFromCamera();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_camera) {
startCameraCapture();
return true;
}
return super.onOptionsItemSelected(item);
}

private void startCameraCapture() {

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {

File photoFile = null;
try {
photoFile = CreateImageFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if(photoFile != null)
{
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(cameraIntent, CAMERA_CAPTURE);
}
}
}

private File CreateImageFile() throws IOException
{
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "Image_" + timeStamp + "_";

File storageDirectory = getExternalFilesDir("");
File image = File.createTempFile(imageFileName, ".jpg",storageDirectory);
return image;

}

@Override
public void onActivityResult(final int requestCode, int resultCode, Intent data) {

switch(requestCode)
{
case CAMERA_CAPTURE:
if(resultCode == RESULT_OK)
{
DisplayCapturedImagesFromCamera();
}
break;
}

}

private void DisplayCapturedImagesFromCamera() {
// TODO Auto-generated method stub
File myPath = getExternalFilesDir(null);
listOfImages = new ArrayList<String>();

try
{

for(File f: myPath.listFiles()) {
listOfImages.add(f.getAbsolutePath());
}

AdptAddjobsGallery adapter = new AdptAddjobsGallery(MainActivity.this,listOfImages);
setListAdapter(adapter);
}
catch(Exception ex)
{
Log.w("Error", ex.getMessage());
}

}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);

// custom dialog
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.cust_dialog);
dialog.setTitle("Image ");

Bitmap bitmap = BitmapFactory.decodeFile(listOfImages.get(position));

// set the custom dialog components - text, image and button
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageBitmap(bitmap);

Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog

dialogButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {

dialog.dismiss();

}
});

dialog.show();
}

}

AdptAddjobsGallery.java

public class AdptAddjobsGallery extends ArrayAdapter<String> {

private final Activity context;

private final ArrayList<String> listOfImages;

public AdptAddjobsGallery(Activity context, ArrayList<String> listOfImages) {
super(context, R.layout.adpt_addjobs_gallery, listOfImages);
// TODO Auto-generated constructor stub

this.context=context;
this.listOfImages = listOfImages;

}

public View getView(int position,View view,ViewGroup parent) {

ViewHolder holder;

if(view == null)
{
LayoutInflater inflater=context.getLayoutInflater();
view =inflater.inflate(R.layout.adpt_addjobs_gallery, null,true);

holder = new ViewHolder();
holder.imageView = (ImageView) view.findViewById(R.id.selfie);
holder.txtTitle = (TextView) view.findViewById(R.id.fileName);
view.setTag(holder);
}
else
{
holder = (ViewHolder) view.getTag();
}


Bitmap bitmap = BitmapFactory.decodeFile(listOfImages.get(position));
File f = new File(listOfImages.get(position));

holder.txtTitle.setText(f.getName());
holder.imageView.setImageBitmap(bitmap);

return view;

};
}

class ViewHolder {
TextView txtTitle;
ImageView imageView;
}

最佳答案

使用file.lastmodified()方法尝试一下。

private ArrayList<String> getRecentImages(long from, long to,
ArrayList<String> list) {
ArrayList<String> sortedList = new ArrayList<String>();
for (int i = 0; i < list.size(); i++) {
File file = new File(list.get(i));
long modified = file.lastModified();
if (modified > from && modified <= to) {
sortedList.add(list.get(i));
}
}
return sortedList;
}

关于java - 从相机捕获图像并设置到android中的 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27667020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com