2016-06-23 3 views
0

最後の画像をファイルから取り出し、ImageViewに配置したいと考えています。ファイルまたはギャラリーから最後の画像を検索して撮影します。 Android Studio(Java)

ここで私は、ファイルを作成します。これが動作するかどうか

public File createImageFile() throws IOException { 
    // Create an image file name 
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); 
    String imageFileName = "JPEG_" + timeStamp + "_"; 
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); 
    File image = File.createTempFile(
      imageFileName, /* prefix */ 
      ".jpg",   /* suffix */ 
      storageDir  /* directory */ 
    ); 

    // Save a file: path for use with ACTION_VIEW intents 
    mCurrentPhotoPath = "file:" + image.getAbsolutePath(); 
    return image; 
}` 
+0

を私はマイファイル/画像から最後の画像をインポートしたいと私はImageViewの上で入れたいです。 –

答えて

1

参照: -

//getting all files within the storage dir (unsorted) 
File[] listFiles = storageDir.listFiles(); 

//a Comparator is used to order Arrays (or other Collections) 
Comparator c = new new Comparator<File>(){ //File-specific Coparator 

    public int compare(File file1, File file2){ 
     //this Comperator uses timestamps for orders 
     long tsFile1 = file1.lastModified(); 
     long tsFile2 = file2.lastModified(); 

     //timestamps are Longs, so we can re-use the Long Comparator 
     return Long.valueOf(tsFile1).compareTo(tsFile2); 
    } 
}; 

//apply the comparator on the array: 
Arrays.sort(listFiles, c); //now it's sorted by day 

//from the sorted array, the last one is the desired file! 
String imgPath = listFiles[listFiles.length-1].getAbsolutePath(); 

//as above 
Bitmap bmImg = BitmapFactory.decodeFile(imgPath); 
imageView.setImageBitmap(bmImg); 
+0

私はそれが2つのアクティビティがあるために働いていないと思います... –

+0

と私は画像の後にファイルを作成します...そして他のアクティビティで私はそれをインポートしようとします –

+1

そう、lastModifiedファイルは常に、 File.lastModified()でファイルを並べ替えるArrays.sort()メソッドの比較器 –

関連する問題