我正在目录中保存照片,我想在recyclerView中显示它们,并在TextView中显示所有图像信息
我的代码 MainActivity:
public class MainActivity extends AppCompatActivity {
RecyclerView rec;
RecyclerViewAdapter recyclerViewAdapter;
List<Items> list = new ArrayList<>();
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
rec= (RecyclerView) findViewById(R.id.recView);
File fi = new File(Environment.getExternalStorageDirectory() + "/PhotosOfSalman");
if (fi.isDirectory()) {
rec.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
recyclerViewAdapter = new RecyclerViewAdapter(list);
rec.setAdapter(recyclerViewAdapter);
}else {
Toast.makeText(getApplicationContext(),"Nothing to show",Toast.LENGTH_LONG).show();
}
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 000);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// if the result is capturing Image
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
bitmap = (Bitmap) data.getExtras().get("data");
savePhoto(bitmap);
Toast.makeText(getApplicationContext(),"Success",Toast.LENGTH_LONG).show();
//Show
}
}
}
public void savePhoto(Bitmap bitmap){
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");
String name = simpleDateFormat.format(new Date());
String directory = Environment.getExternalStorageDirectory().toString();
File folder = new File(directory+"/PhotosOfSalman");
folder.mkdirs();
File myFile = new File(folder,name+".JPEG");
try {
FileOutputStream stream = new FileOutputStream(myFile);
bitmap.compress(Bitmap.CompressFormat.JPEG,80,stream);
stream.flush();
stream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Recycler View适配器类: -
public class RecyclerViewAdapter extends RecyclerView.Adapter<ViewHolder> {
List<Items> list;
public RecyclerViewAdapter(List<Items> list) {
this.list = list;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Items items=list.get(position);
holder.name.setText(items.getName());
holder.date.setText(items.getDate());
holder.size.setText(items.getSize());
holder.imageView.setImageBitmap(items.getImage());
}
@Override
public int getItemCount() {
return list.size();
}
}
项目类: -
public class Items {
Bitmap image;
String name;
String date;
String size;
public Bitmap getImage() {
return image;
}
public void setImage(Bitmap image) {
this.image = image;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getSize() {
return size;
}
public void setSize(String size) {
this.size = size;
}
public Items() {
}
public Items(Bitmap image, String name, String date, String size) {
this.image = image;
this.name = name;
this.date = date;
this.size = size;
}
}
任何帮助?