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

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

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&lt;?&gt; adapterView, View view, int i, long l) {
Intent openImage = new Intent(getActivity(), FullImageActivity.class);
openImage.putExtra(&quot;Img :&quot;, imageList.get(i).toString());
startActivity(openImage);
}
});
}
else
{
Toast.makeText(getActivity(), &quot;No image Found&quot;, Toast.LENGTH_SHORT).show();
}
}
public void imageNameList ()
{
imageNameList = new ArrayList&lt;String&gt;();
File imagePathForName = new File(Environment.getExternalStorageDirectory() );
Cursor cursor = getContext().getContentResolver().query(Uri.fromFile(imagePathForName), null, null, null, null);
if (cursor == null)
{
Toast.makeText(getActivity(), &quot;No Image File Found&quot;, Toast.LENGTH_SHORT).show();
} else
{
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DISPLAY_NAME);
imageNameList.add(cursor.getString(idx));
Log.i( &quot;FineName: &quot; , 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(&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:

确定