在我的应用中,我需要在每次加载应用时在图库中显示最后添加的图像。我能够使用applicationDataDirectory中的图像来完成它,但这不是我的要求。我需要从整个画廊图像。我该怎么做。提前谢谢。
var dir = Ti.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory);
var fdir = dir.getDirectoryListing();
var numPhotos = fdir.length || 1;
for(var a=0; a < fdir.length; a++ ) {
Ti.API.info("inside for");
Ti.API.info("filename"+fdir[a]);
var f = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory,fdir[a]);
if(f.exists()){
var fr = f.read();
Ti.API.info("fr.mimeType"+fr.mimeType);
if(fr.mimeType === 'image/jpeg'){
Ti.API.info('photo.nativePath: ' + f.nativePath);
imagePath = f.nativePath;
break;
}
}
}
答案 0 :(得分:-3)
我认为以下代码适用于你......
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE
};
final Cursor cursor = getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
// Put it in the image view
if (cursor.moveToFirst()) {
final ImageView imageView = (ImageView) findViewById(R.id.pictureView);
String imageLocation = cursor.getString(1);
File imageFile = new File(imageLocation);
if (imageFile.exists()) { // TODO: is there a better way to do this?
Bitmap bm = BitmapFactory.decodeFile(imageLocation);
imageView.setImageBitmap(bm);
}
}