从数组列表获取文件名 [英] Get file name from array list

查看:333
本文介绍了从数组列表获取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么会去从这个PageAdapter获取文件名,所以我可以用它作为一个字符串,删除图像。

我试过转换viewPager.setCurrentItem(位置)为一个字符串,我试图转换viewPager.getCurrentItem(),我尝试了位图转换为String ......我出出主意,现在轮到你的专家。

我一直在寻找计算器和谷歌3天,找不到任何东西来帮助我这个问题。

任何帮助,您可以提供将大大AP preciated。

FullScreenViewActivity.java

 包com.company.imageslider;进口com.company.imageslider.adapter.FullScreenImageAdapter;
进口com.company.imageslider.helper.Utils;
进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.support.v4.view.ViewPager;公共类FullScreenViewActivity延伸活动{    私人utils的utils的;
    私人FullScreenImageAdapter适配器;
    私人ViewPager viewPager;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_fullscreen_view);        viewPager =(ViewPager)findViewById(R.id.pager);        utils的=新utils的(getApplicationContext());        意向I = getIntent();
        INT位置= i.getIntExtra(位置,0);        适配器=新FullScreenImageAdapter(FullScreenViewActivity.this,
                utils.getFilePaths());        viewPager.setAdapter(适配器);        //首先显示所选图像
        viewPager.setCurrentItem(位置);
    }
}

FullScreenImageAdapter.java

 包com.company.imageslider.adapter;进口com.company.imageslider.R;
进口com.company.imageslider.helper.TouchImageView;进口的java.util.ArrayList;进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.support.v4.view.PagerAdapter;
进口android.support.v4.view.ViewPager;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.ImageView;
进口android.widget.RelativeLayout;公共类FullScreenImageAdapter扩展PagerAdapter {    私人活动_activity;
    私人的ArrayList<串GT; _imagePaths;
    私人LayoutInflater吹气;    //构造
    公共FullScreenImageAdapter(活动活动,
            ArrayList的<串GT; imagePaths){
        this._activity =活动;
        this._imagePaths = imagePaths;
    }    @覆盖
    公众诠释的getCount(){
        返回this._imagePaths.size();
    }    @覆盖
    公共布尔isViewFromObject(查看视图,Object对象){
        返回查看==((RelativeLayout的)对象);
    }    @覆盖
    公共对象instantiateItem(ViewGroup中的容器,INT位置){
        TouchImageView imgDisplay;
        按钮btnClose;        吹气=(LayoutInflater)_activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        查看viewLayout = inflater.inflate(R.layout.layout_fullscreen_image,集装箱,
                假);        imgDisplay =(TouchImageView)viewLayout.findViewById(R.id.imgDisplay);
        btnClose =(按钮)viewLayout.findViewById(R.id.btnClose);        BitmapFactory.Options选项=新BitmapFactory.Options();
        options.in preferredConfig = Bitmap.Config.ARGB_8888;
        位图位图= BitmapFactory.de codeFILE(_imagePaths.get(位置),选件);
        imgDisplay.setImageBitmap(位图);        //关闭按钮单击事件
        btnClose.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                _activity.finish();
            }
        });        ((ViewPager)容器).addView(viewLayout);        返回viewLayout;
    }    @覆盖
    公共无效destroyItem(ViewGroup中的容器,INT位置,Object对象){
        ((ViewPager)容器).removeView((RelativeLayout的)对象);    }}

Utils.java

 包com.company.imageslider.helper;进口的java.io.File;
进口的java.util.ArrayList;
进口java.util.Locale中;进口android.app.AlertDialog;
进口android.content.Context;
进口android.graphics.Point;
进口android.view.Display;
进口android.view.WindowManager;
进口android.widget.Toast;公共类的Utils {    私人语境_context;    //构造
    公共utils的(上下文的背景下){
        this._context =背景;
    }    / *
     *阅读文件从SD卡路径
     * /
    公众的ArrayList<串GT; getFilePaths(){
        ArrayList的<串GT;文件路径=新的ArrayList<串GT;();        文件目录=新的文件(
                android.os.Environment.getExternalStorageDirectory()
                        +文件分割符+ AppConstant.PHOTO_ALBUM);        //检查目录
        如果(directory.isDirectory()){
            //获取文件路径列表
            文件[] listFiles = directory.listFiles();            //检查计数
            如果(listFiles.length大于0){                //通过所有文件循环
                的for(int i = 0; I< listFiles.length;我++){                    //获取文件路径
                    字符串文件路径= listFiles [I] .getAbsolutePath();                    //检查支持的文件扩展名
                    如果(IsSupportedFile(文件路径)){
                        //映像路径添加到数组列表
                        filePaths.add(文件路径);
                    }
                }
            }其他{
                //图像目录是空的
                Toast.makeText(
                        _context,
                        AppConstant.PHOTO_ALBUM
                                +是空的,请加载一些图像呢!
                        Toast.LENGTH_LONG).show();
            }        }其他{
            AlertDialog.Builder警报=新AlertDialog.Builder(_context);
            alert.setTitle(错误!);
            alert.setMessage(AppConstant.PHOTO_ALBUM
                    +目录路径是无效的,请设置图像的目录名AppConstant.java类!);
            alert.setPositiveButton(OK,NULL);
            alert.show();
        }        返回文件路径;
    }    / *
     *检查支持的文件扩展名
     *
     * @Returns布尔
     * /
    私人布尔IsSupportedFile(字符串文件路径){
        串EXT = filePath.substring((filePath.lastIndexOf(。)+1),
                filePath.length());        如果(AppConstant.FILE_EXTN
                。载(ext.toLowerCase(Locale.getDefault())))
            返回true;
        其他
            返回false;    }    / *
     *让屏幕宽度
     * /
    公众诠释getScreenWidth(){
        INT columnWidth时;
        窗口管理WM =(窗口管理器)_context
                .getSystemService(Context.WINDOW_SERVICE);
        显示显示= wm.getDefaultDisplay();        最后一点点=新点();
        尝试{
            display.getSize(点);
        }赶上(java.lang.NoSuchMethodError忽略){//旧设备
            point.x = display.getWidth();
            point.y = display.getHeight();
        }
        columnWidth时= point.x;
        返回columnWidth时;
    }
}

AppConstant.java

 包com.company.imageslider.helper;进口java.util.Arrays中;
进口的java.util.List;公共类AppConstant {    //的网格视图的列数
    公共静态最终诠释NUM_OF_COLUMNS = 3;    // GridView的图象填补
    公共静态最终诠释GRID_PADDING = 8; //在DP    // SD卡图像目录
    公共静态最后弦乐PHOTO_ALBUM =NAT;    //支持的文件格式
    公共静态最后的名单,LT;弦乐> FILE_EXTN = Arrays.asList(JPG,JPEG,
            PNG);
}


解决方案

您没有得到来自适配器图像路径。您可以从您的数组列表得到它:_imagePaths

使用

 字符串路径= _imagePaths.get(mViewPager.getCurrentItem());

How would I go about getting the File name from this PageAdapter so I can use it as a string to delete an image.

I tried converting viewPager.setCurrentItem(position) to a string, I tried converting viewPager.getCurrentItem(), I tried converting the Bitmap to a String... I am out of ideas and now turn to you experts.

I have been search stackoverflow and Google for 3 days and can not find anything to help me with this.

Any Help that you can provide would be greatly appreciated.

FullScreenViewActivity.java

package com.company.imageslider;

import com.company.imageslider.adapter.FullScreenImageAdapter;
import com.company.imageslider.helper.Utils;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;

public class FullScreenViewActivity extends Activity{

    private Utils utils;
    private FullScreenImageAdapter adapter;
    private ViewPager viewPager;

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

        viewPager = (ViewPager) findViewById(R.id.pager);

        utils = new Utils(getApplicationContext());

        Intent i = getIntent();
        int position = i.getIntExtra("position", 0);

        adapter = new FullScreenImageAdapter(FullScreenViewActivity.this,
                utils.getFilePaths());

        viewPager.setAdapter(adapter);

        // displaying selected image first
        viewPager.setCurrentItem(position);
    }
}

FullScreenImageAdapter.java

package com.company.imageslider.adapter;

import com.company.imageslider.R;
import com.company.imageslider.helper.TouchImageView;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class FullScreenImageAdapter extends PagerAdapter {

    private Activity _activity;
    private ArrayList<String> _imagePaths;
    private LayoutInflater inflater;

    // constructor
    public FullScreenImageAdapter(Activity activity,
            ArrayList<String> imagePaths) {
        this._activity = activity;
        this._imagePaths = imagePaths;
    }

    @Override
    public int getCount() {
        return this._imagePaths.size();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == ((RelativeLayout) object);
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        TouchImageView imgDisplay;
        Button btnClose;

        inflater = (LayoutInflater) _activity
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View viewLayout = inflater.inflate(R.layout.layout_fullscreen_image, container,
                false);

        imgDisplay = (TouchImageView) viewLayout.findViewById(R.id.imgDisplay);
        btnClose = (Button) viewLayout.findViewById(R.id.btnClose);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(_imagePaths.get(position), options);
        imgDisplay.setImageBitmap(bitmap);

        // close button click event
        btnClose.setOnClickListener(new View.OnClickListener() {            
            @Override
            public void onClick(View v) {
                _activity.finish();
            }
        }); 

        ((ViewPager) container).addView(viewLayout);

        return viewLayout;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container).removeView((RelativeLayout) object);

    }

}

Utils.java

package com.company.imageslider.helper;

import java.io.File;
import java.util.ArrayList;
import java.util.Locale;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Point;
import android.view.Display;
import android.view.WindowManager;
import android.widget.Toast;

public class Utils {

    private Context _context;

    // constructor
    public Utils(Context context) {
        this._context = context;
    }

    /*
     * Reading file paths from SDCard
     */
    public ArrayList<String> getFilePaths() {
        ArrayList<String> filePaths = new ArrayList<String>();

        File directory = new File(
                android.os.Environment.getExternalStorageDirectory()
                        + File.separator + AppConstant.PHOTO_ALBUM);

        // check for directory
        if (directory.isDirectory()) {
            // getting list of file paths
            File[] listFiles = directory.listFiles();

            // Check for count
            if (listFiles.length > 0) {

                // loop through all files
                for (int i = 0; i < listFiles.length; i++) {

                    // get file path
                    String filePath = listFiles[i].getAbsolutePath();

                    // check for supported file extension
                    if (IsSupportedFile(filePath)) {
                        // Add image path to array list
                        filePaths.add(filePath);
                    }
                }
            } else {
                // image directory is empty
                Toast.makeText(
                        _context,
                        AppConstant.PHOTO_ALBUM
                                + " is empty. Please load some images in it !",
                        Toast.LENGTH_LONG).show();
            }

        } else {
            AlertDialog.Builder alert = new AlertDialog.Builder(_context);
            alert.setTitle("Error!");
            alert.setMessage(AppConstant.PHOTO_ALBUM
                    + " directory path is not valid! Please set the image directory name AppConstant.java class");
            alert.setPositiveButton("OK", null);
            alert.show();
        }

        return filePaths;
    }

    /*
     * Check supported file extensions
     * 
     * @returns boolean
     */
    private boolean IsSupportedFile(String filePath) {
        String ext = filePath.substring((filePath.lastIndexOf(".") + 1),
                filePath.length());

        if (AppConstant.FILE_EXTN
                .contains(ext.toLowerCase(Locale.getDefault())))
            return true;
        else
            return false;

    }

    /*
     * getting screen width
     */
    public int getScreenWidth() {
        int columnWidth;
        WindowManager wm = (WindowManager) _context
                .getSystemService(Context.WINDOW_SERVICE);
        Display display = wm.getDefaultDisplay();

        final Point point = new Point();
        try {
            display.getSize(point);
        } catch (java.lang.NoSuchMethodError ignore) { // Older device
            point.x = display.getWidth();
            point.y = display.getHeight();
        }
        columnWidth = point.x;
        return columnWidth;
    }
}

AppConstant.java

package com.company.imageslider.helper;

import java.util.Arrays;
import java.util.List;

public class AppConstant {

    // Number of columns of Grid View
    public static final int NUM_OF_COLUMNS = 3;

    // Gridview image padding
    public static final int GRID_PADDING = 8; // in dp

    // SD card image directory
    public static final String PHOTO_ALBUM = "NAT";

    // supported file formats
    public static final List<String> FILE_EXTN = Arrays.asList("jpg", "jpeg",
            "png");
}

解决方案

You don't get the image path from the adapter. You get it from your array list: _imagePaths

use

 String path = _imagePaths.get(mViewPager.getCurrentItem());

这篇关于从数组列表获取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆