如何从文件中获取图像和图像名称?

huangapple go评论99阅读模式
英文:

How to get Image and Image Name from the file?

问题

  1. public void imageList() {
  2. File imagePathForImage = new File(Environment.getExternalStorageDirectory());
  3. imageList = imageReader(imagePathForImage);
  4. if (imageList.size() != 0) {
  5. gallery.setAdapter(new imageAdapater());
  6. gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  7. @Override
  8. public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
  9. Intent openImage = new Intent(getActivity(), FullImageActivity.class);
  10. openImage.putExtra("Img :", imageList.get(i).toString());
  11. startActivity(openImage);
  12. }
  13. });
  14. } else {
  15. Toast.makeText(getActivity(), "No image Found", Toast.LENGTH_SHORT).show();
  16. }
  17. }
  18. public void imageNameList() {
  19. imageNameList = new ArrayList<String>();
  20. File imagePathForName = new File(Environment.getExternalStorageDirectory());
  21. Cursor cursor = getContext().getContentResolver().query(Uri.fromFile(imagePathForName), null, null, null, null);
  22. if (cursor == null) {
  23. Toast.makeText(getActivity(), "No Image File Found", Toast.LENGTH_SHORT).show();
  24. } else {
  25. cursor.moveToFirst();
  26. int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME);
  27. imageNameList.add(cursor.getString(idx));
  28. Log.i("FineName: ", cursor.getString(idx));
  29. cursor.close();
  30. }
  31. }
  32. 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 ?

  1. public void imageList()
  2. {
  3. File imagePathForImage = new File(Environment.getExternalStorageDirectory());
  4. imageList = imageReader(imagePathForImage);
  5. if(imageList.size() != 0)
  6. {
  7. gallery.setAdapter(new imageAdapater());
  8. gallery.setOnItemClickListener(new AdapterView.OnItemClickListener() {
  9. @Override
  10. public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
  11. Intent openImage = new Intent(getActivity(), FullImageActivity.class);
  12. openImage.putExtra(&quot;Img :&quot;, imageList.get(i).toString());
  13. startActivity(openImage);
  14. }
  15. });
  16. }
  17. else
  18. {
  19. Toast.makeText(getActivity(), &quot;No image Found&quot;, Toast.LENGTH_SHORT).show();
  20. }
  21. }
  22. public void imageNameList ()
  23. {
  24. imageNameList = new ArrayList&lt;String&gt;();
  25. File imagePathForName = new File(Environment.getExternalStorageDirectory() );
  26. Cursor cursor = getContext().getContentResolver().query(Uri.fromFile(imagePathForName), null, null, null, null);
  27. if (cursor == null)
  28. {
  29. Toast.makeText(getActivity(), &quot;No Image File Found&quot;, Toast.LENGTH_SHORT).show();
  30. } else
  31. {
  32. cursor.moveToFirst();
  33. int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME);
  34. imageNameList.add(cursor.getString(idx));
  35. Log.i( &quot;FineName: &quot; , cursor.getString(idx) );
  36. cursor.close();
  37. }
  38. }

my plan is to create gallery of images showing respective names below it.

答案1

得分: 0

你可以从路径中获取:

  1. String filename = path.substring(path.lastIndexOf("/") + 1);
英文:

you can get from path

  1. String filename=path.substring(path.lastIndexOf(&quot;/&quot;)+1);

huangapple
  • 本文由 发表于 2020年5月3日 23:59:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/61577499.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定