1

要从图库中获取图像,我使用了以下代码,

String[] projection = new String[]{
    MediaStore.Images.Media._ID,
    MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
    MediaStore.Images.Media.DATE_TAKEN,
    MediaStore.Images.Media.DATA,
    MediaStore.Images.Media.DEFAULT_SORT_ORDER
};

Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

Cursor cur = managedQuery(images,
    projection, // Which columns to return
    "",         // Which rows to return (all rows)
    null,       // Selection arguments (none)
    ""          // Ordering
    );

if (cur.moveToFirst()) {
String bucket;
String id;
String filePath;
int bucketColumn = cur.getColumnIndex(
    MediaStore.Images.Media.BUCKET_DISPLAY_NAME);

int dateColumn = cur.getColumnIndex(
    MediaStore.Images.Media._ID);
int dataColumnIndex = cur.getColumnIndex(MediaStore.Images.Media.DATA);

do {
    // Get the field values
    bucket = cur.getString(bucketColumn);
    id = cur.getString(dateColumn);
    data=cur.getString(dataColumnIndex);

    // Do something with the values.
    Log.i("ListingImages", " bucket=" + bucket 
       + "  date_taken=" + id + "Data = "+filePath);

} while (cur.moveToNext());

}

然后使用位图采样方法在网格视图中显示图像

private Bitmap decodeFile(String filePath){
        //decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, o);

        //Find the correct scale value. It should be the power of 2.
        final int REQUIRED_SIZE=70;

        int width_tmp=o.outWidth, height_tmp=o.outHeight;
        int scale=1;

        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
    scale*=2;

        //decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;
        Bitmap bitmap=BitmapFactory.decodeFile(filePath, o2);

        return bitmap;
}

然后只想从网格视图中选择图像,然后将选择的图像存储到不同尺寸的 SD 卡中,如 640*480 和 120*90,这意味着一般图像和缩略图图像

要将图像存储到 SD 卡中,我使用了以下代码,

File root = new File(Environment.getExternalStorageDirectory()+ "/wo_photos");

if(!root.exists())
     root.mkdirs();


Bitmap selectedBitmap=Bitmap.createBitmap(yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), Bitmap.Config.RGB_565);

Canvas canvas = new Canvas(selectedBitmap);
    Rect dstRect = new Rect(0,0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight());

    canvas.drawBitmap(yourSelectedImage, dstRect, dstRect, null);

ByteArrayOutputStream bytes = new ByteArrayOutputStream();

Bitmap bitmap=Bitmap.createScaledBitmap(selectedBitmap, 640, 480, false);

bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);

//you can create a new file name "test.jpg" in sdcard folder.
File f = new File(root,pos+"wo_image.jpg");
f.createNewFile();
//write the bytes in file

FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());

// remember close de FileOutput
fo.close();
fOut.close();



When i see images in SD Card it has quality loss.However when i store images without sampling bitmap it gives
OutOfMemory exeption.I belive that quality loss in images by giving sampling size.

To get images without quality loss what should i do?

Thanks,
4

0 回答 0