如何获得拍摄日期和显示呢? [英] How to get the Date Taken and Display it?

查看:93
本文介绍了如何获得拍摄日期和显示呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的活动,其中的第一个活动是展现在SD卡中的照片并将其加载到GridView控件。而第二个活动是当你点击在GridView的图片,它会显示图像的完整大小。什么是我要的是我的第二个活动,我想也显示被点击图片的拍摄日期。如何获得拍摄日期和显示它。

下面是我的第一个活动。

 公共类MainActivity延伸活动{

公共类ImageAdapter扩展了BaseAdapter {

    私人语境mContext;
    ArrayList的<字符串> ITEMLIST =新的ArrayList<字符串>();

    公共ImageAdapter(上下文C){
        mContext = C;
    }

    空加(字符串路径){
        itemList.add(路径);
    }

    @覆盖
    公众诠释getCount将(){
        返回itemList.size();
    }

    @覆盖
    公共对象的getItem(INT为arg0){
        // TODO自动生成方法存根
        返回null;
    }

    @覆盖
    众长getItemId(INT位置){
        // TODO自动生成方法存根
        返回0;
    }

    @覆盖
    公共查看getView(INT位置,查看convertView,ViewGroup中父){
        ImageView的ImageView的;
        如果(convertView == NULL){//如果它不回收,初始化一些
                                    //属性
            ImageView的=新ImageView的(mContext);
            imageView.setLayoutParams(新GridView.LayoutParams(220,220));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8,8,8,8);
        } 其他 {
            ImageView的=(ImageView的)convertView;
        }

        位图BM =去codeSampledBitmapFromUri(itemList.get(位置),220,
                220);

        imageView.setImageBitmap(BM);
        返回ImageView的;
    }

    公共位图德codeSampledBitmapFromUri(字符串路径,诠释reqWidth,
            INT reqHeight){

        位图BM = NULL;
        //第一代code与inJustDe codeBounds = true来检查尺寸
        最后BitmapFactory.Options选项=新BitmapFactory.Options();
        options.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeFILE(路径选择);

        //计算inSampleSize
        options.inSampleSize = calculateInSampleSize(选项,reqWidth,
                reqHeight);

        与inSampleSize集//德code位图
        options.inJustDe codeBounds = FALSE;
        BM = BitmapFactory.de codeFILE(路径选择);

        返回BM;
    }

    公众诠释calculateInSampleSize(

    BitmapFactory.Options选项,INT reqWidth,诠释reqHeight){
        //原始高度和宽度的图像
        最终诠释身高= options.outHeight;
        最终诠释宽度= options.outWidth;
        INT inSampleSize = 1;

        如果(高度> reqHeight ||宽度GT; reqWidth){
            如果(宽>高度){
                inSampleSize = Math.round((浮点)高度
                        /(浮点)reqHeight);
            } 其他 {
                inSampleSize = Math.round((浮点)宽/(浮点)reqWidth);
            }
        }

        返回inSampleSize;
    }

}

ImageAdapter myImageAdapter;

@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);
    myImageAdapter =新ImageAdapter(本);
    gridview.setAdapter(myImageAdapter);

    字符串ExternalStorageDirectoryPath =环境
            .getExternalStorageDirectory()getAbsolutePath()。

    字符串TARGETPATH​​ = ExternalStorageDirectoryPath +/测试/;

    Toast.makeText(getApplicationContext(),TARGETPATH​​,Toast.LENGTH_LONG)
            。显示();
    文件targetDirector =新的文件(TARGETPATH​​);

    文件[]文件= targetDirector.listFiles();
    对于(文件文件:文件){
        myImageAdapter.add(file.getAbsolutePath());
    }
}

gridview.setOnItemClickListener(新OnItemClickListener(){
            公共无效onItemClick(适配器视图<>母公司,视图V,
                    INT位置,长的id){

ImageView的IMG = myImageAdapter.getView(位置,V,父母);
                img.buildDrawingCache();
                位图BMAP = img.getDrawingCache();
                意向意图=新的意图(MainActivity.this,
                        Imageviewer.class);
                束束=新包();
                    字符串面值= myimageadpter.getpath(位置);
                             bundle.putString(的ImagePath,标准杆);
                intent.putExtras(包);
                startActivityForResult(意向,0);

            }
        });
 

这里是第二个活动

 公共类图像浏览扩展活动{
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);


    束束= this.getIntent()getExtras()。

    字符串s = bundle.getString(的ImagePath);
    位图Imagefrompath = BitmapFactory.de codeFILE(S);
            ImageView的IMG =(ImageView的)findViewById(R.id.imageView1);
            img.setImageBitmap(Imagefrompath);


}

}
 

解决方案

您可以看到最佳答案的第一个环节,我在这里复制的第一部分。第二个也与此有关。

<一个href="http://stackoverflow.com/questions/12478736/display-the-date-of-the-picture-when-it-was-taken">Display画面是拍摄时的日期?

Android的:如何获取文件的创建日期

 文件文件=新的文件(文件路径);
如果(file.exists())//额外的检查,只是为了验证给出的路径
{
    日期lastModDate =新的日期(file.lastModified());
    Log.i(年月日:+ lastModDate.toString()); // Dispaly lastModDate。你可以做/使用它自己的路
}
 

文件路径=>图像的位置在SD卡(连同完整的图像名(包括扩展名))

更新:

(来自以上给出的第一连杆)。另一种方法是检查图像上的EXIF数据,以获得照片拍摄的日期,如果该信息:

  ExifInterface INTF = NULL;
尝试
{
    INTF =新ExifInterface(路径);
}
赶上(IOException异常E)
{
    e.printStackTrace();
}

如果(INTF!= NULL)
{
    字符串dateString = intf.getAttribute(ExifInterface.TAG_DATETIME);
    Log.i(年月日:+ dateString.toString()); // Dispaly dateString。你可以做/使用它自己的路
}
 

I have this Activities where in the First Activity is to show the pictures in sdcard and load it in gridview. While the second activity is when you click a picture in the gridview it'll display the full size of the image. What do I want is with my second activity, I want also to display the 'date taken' of the picture that was clicked. How to get the date taken and display it.

Here is my first Activity.

public class MainActivity extends Activity {

public class ImageAdapter extends BaseAdapter {

    private Context mContext;
    ArrayList<String> itemList = new ArrayList<String>();

    public ImageAdapter(Context c) {
        mContext = c;
    }

    void add(String path) {
        itemList.add(path);
    }

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

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView;
        if (convertView == null) { // if it's not recycled, initialize some
                                    // attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220,
                220);

        imageView.setImageBitmap(bm);
        return imageView;
    }

    public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
            int reqHeight) {

        Bitmap bm = null;
        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        bm = BitmapFactory.decodeFile(path, options);

        return bm;
    }

    public int calculateInSampleSize(

    BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            if (width > height) {
                inSampleSize = Math.round((float) height
                        / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }

        return inSampleSize;
    }

}

ImageAdapter myImageAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    myImageAdapter = new ImageAdapter(this);
    gridview.setAdapter(myImageAdapter);

    String ExternalStorageDirectoryPath = Environment
            .getExternalStorageDirectory().getAbsolutePath();

    String targetPath = ExternalStorageDirectoryPath + "/test/";

    Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG)
            .show();
    File targetDirector = new File(targetPath);

    File[] files = targetDirector.listFiles();
    for (File file : files) {
        myImageAdapter.add(file.getAbsolutePath());
    }
}

gridview.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v,
                    int position, long id) {

ImageView img = myImageAdapter.getView(position, v, parent);
                img.buildDrawingCache(); 
                Bitmap bmap = img.getDrawingCache();
                Intent intent = new Intent(MainActivity.this,
                        Imageviewer.class);
                Bundle bundle = new Bundle();
                    String par=myimageadpter.getpath(position);
                             bundle.putString("imagepath", par);
                intent.putExtras(bundle);
                startActivityForResult(intent, 0);

            }
        });

And here is the second Activity

public class ImageViewer extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);


    Bundle bundle = this.getIntent().getExtras();

    String s=bundle.getString("imagepath");
    Bitmap Imagefrompath = BitmapFactory.decodeFile(s);
            ImageView img=(ImageView) findViewById(R.id.imageView1);
            img.setImageBitmap(Imagefrompath );


}

}

解决方案

You can see the first link for the best answer, I have copied its first part here. 2nd also relevant

Display the date of the picture when it was taken?

Android: How to get a file's creation date?

File file = new File(filePath);
if(file.exists()) //Extra check, Just to validate the given path
{
    Date lastModDate = new Date(file.lastModified());    
    Log.i("Dated : "+ lastModDate.toString());//Dispaly lastModDate. You can do/use it your own way
}

filepath=>location of image in sdcard (along with complete image name (including extension))

Update :

(From the first link given above). The other way would be to check the EXIF data on the image to get the date the picture was taken if that information is available:

ExifInterface intf = null;
try 
{
    intf = new ExifInterface(path);
}
catch(IOException e)
{
    e.printStackTrace();
}

if(intf != null)
{
    String dateString = intf.getAttribute(ExifInterface.TAG_DATETIME);
    Log.i("Dated : "+ dateString.toString()); //Dispaly dateString. You can do/use it your own way
}

这篇关于如何获得拍摄日期和显示呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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