我使用下面的代码来获取sd卡中的图像文件路径。
File[] f = (Environment.getExternalStorageDirectory()).listFiles();
int i;
for(i = 0; i < f.length; i++) {
if(f[i].isFile()) {
if(isPhoto(f[i].getName())) {
Filepath.add(f[i].getAbsolutePath());
}
}
else {
//recursive
}
}
我想通过知道原始图像路径获取图像缩略图的路径。 怎么做?
答案 0 :(得分:3)
你可以从这样的文件中获取Uri:
Uri uri = Uri.fromFile(new File("/mnt/images/abc.jpg"));
Bitmap thumbnail = getPreview(uri);
And the following function gives you the thumbnail:
Bitmap getPreview(URI uri) {
File image = new File(uri);
BitmapFactory.Options bounds = new BitmapFactory.Options();
bounds.inJustDecodeBounds = true;
BitmapFactory.decodeFile(image.getPath(), bounds);
if ((bounds.outWidth == -1) || (bounds.outHeight == -1))
return null;
int originalSize = (bounds.outHeight > bounds.outWidth) ? bounds.outHeight
: bounds.outWidth;
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = originalSize / THUMBNAIL_SIZE;
return BitmapFactory.decodeFile(image.getPath(), opts);
}
答案 1 :(得分:2)
您可以制作自己的缩略图:
byte[] imageData = null;
try
{
final int THUMBNAIL_SIZE = 64;
FileInputStream fis = new FileInputStream(fileName);
Bitmap imageBitmap = BitmapFactory.decodeStream(fis);
imageBitmap = Bitmap.createScaledBitmap(imageBitmap, THUMBNAIL_SIZE, THUMBNAIL_SIZE, false);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
}
catch(Exception ex) {
}