英文:
How to get Image and Image Name from the file?
问题
public void imageList() {
File imagePathForImage = new File(Environment.getExternalStorageDirectory());
imageList = imageReader(imagePathForImage);
if (imageList.size() != 0) {
gallery.setAdapter(new imageAdapater());
gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent openImage = new Intent(getActivity(), FullImageActivity.class);
openImage.putExtra("Img :", imageList.get(i).toString());
startActivity(openImage);
}
});
} else {
Toast.makeText(getActivity(), "No image Found", Toast.LENGTH_SHORT).show();
}
}
public void imageNameList() {
imageNameList = new ArrayList<String>();
File imagePathForName = new File(Environment.getExternalStorageDirectory());
Cursor cursor = getContext().getContentResolver().query(Uri.fromFile(imagePathForName), null, null, null, null);
if (cursor == null) {
Toast.makeText(getActivity(), "No Image File Found", Toast.LENGTH_SHORT).show();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME);
imageNameList.add(cursor.getString(idx));
Log.i("FineName: ", cursor.getString(idx));
cursor.close();
}
}
my plan is to create a gallery of images showing respective names below it.
英文:
I set below codes for fragment tab. I am trying to get image and it's name from the file and adding in separate arraylists. I am getting images successfully but failed to get respective names. Everytime cursor is being null. What can I do ?
public void imageList()
{
File imagePathForImage = new File(Environment.getExternalStorageDirectory());
imageList = imageReader(imagePathForImage);
if(imageList.size() != 0)
{
gallery.setAdapter(new imageAdapater());
gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent openImage = new Intent(getActivity(), FullImageActivity.class);
openImage.putExtra("Img :", imageList.get(i).toString());
startActivity(openImage);
}
});
}
else
{
Toast.makeText(getActivity(), "No image Found", Toast.LENGTH_SHORT).show();
}
}
public void imageNameList ()
{
imageNameList = new ArrayList<String>();
File imagePathForName = new File(Environment.getExternalStorageDirectory() );
Cursor cursor = getContext().getContentResolver().query(Uri.fromFile(imagePathForName), null, null, null, null);
if (cursor == null)
{
Toast.makeText(getActivity(), "No Image File Found", Toast.LENGTH_SHORT).show();
} else
{
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME);
imageNameList.add(cursor.getString(idx));
Log.i( "FineName: " , cursor.getString(idx) );
cursor.close();
}
}
my plan is to create gallery of images showing respective names below it.
答案1
得分: 0
你可以从路径中获取:
String filename = path.substring(path.lastIndexOf("/") + 1);
英文:
you can get from path
String filename=path.substring(path.lastIndexOf("/")+1);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论