解码位图列表

时间:2014-11-30 10:31:43

标签: android picasso android-bitmap

我有一个分辨率为X x Y的位图列表。我想将每个图像调整为M x N并将其保存到磁盘。 我是在AsyncTask中做的:

public class MyAsyncTask extends AsyncTask<Void, Void, Void>{

        Cursor mCursor;
        SQLiteDatabase db;
        private final int WIDTH;
        private final int HEIGHT;
        private final String ASSET_PREFIX = "assets://";
        private final List<Target> list;

        public MyAsyncTask(Cursor cursor, int width, int height, SQLiteDatabase db){
            mCursor = cursor;
            WIDTH = width;
            HEIGHT = height;
            this.db = db;
            list = new ArrayList<Target>();
        }

        @Override
        protected Void doInBackground(Void... voids) {
            //get list of image entity from db
            final List<ImageEntity> imageEntityList = getImageEntries(mCursor);

            //scale every image to needed size
            for(int i = 0; i < imageEntityList.size(); i++){
                final ImageEntity entity = imageEntityList.get(i);
                final String imageName = imageEntityList.get(i).
                        getImageUrl().replace(ASSET_PREFIX, "");

                final String uriPicture = "file:///android_asset/" + imageName;
                MainActivity.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        Target target = new ScaledImageTarget(entity, WIDTH, HEIGHT, imageName, db);
                        list.add(target);
                        Log.e(LOG_TAG, target.hashCode() + "");
                        Picasso.with(HealfieApplication.getContext()).
                                load(uriPicture).
                                into(target);
                    }
                });
            }
            return null;
        }

        public List<ImageEntity> getImageEntries(Cursor cursor){
            List<ImageEntity> result = new ArrayList<ImageEntity>();

            if(cursor.moveToFirst()){
                int id = 1;
                do{
                    int columnIdIndex = cursor.getColumnIndex(PhotoContract.PhotoEntry._ID);
                    int columnPhotoUrlIndex = cursor.getColumnIndex(PhotoContract.PhotoEntry.COLUMN_PHOTO_URL);
                    int columnScaledPhotoUrlIndex = cursor.getColumnIndex(PhotoContract.PhotoEntry.COLUMN_SCALED_PHOTO_URL);
                    int columnDateIndex = cursor.getColumnIndex(PhotoContract.PhotoEntry.COLUMN_DATE);
                    int columnWeightIndex = cursor.getColumnIndex(PhotoContract.PhotoEntry.COLUMN_WEIGHT);
                    if(columnIdIndex != -1 && columnPhotoUrlIndex != -1 && columnDateIndex != -1 && columnWeightIndex != -1){
                        ImageEntity entity = new ImageEntity(
                                cursor.getString(columnIdIndex),
                                cursor.getString(columnPhotoUrlIndex),
                                cursor.getString(columnScaledPhotoUrlIndex),
                                cursor.getString(columnWeightIndex),
                                cursor.getString(columnDateIndex)
                        );
                        result.add(entity);
                    }
                    id++;
                }while(!cursor.isLast() && cursor.moveToNext());
            }
            return result;
        }
    }

我的目标对象的代码从毕加索目标延伸:

private class ScaledImageTarget implements Target {
private ImageEntity entity;
private int width;
private int height;
private boolean isLoaded;
private String imageName;
private SQLiteDatabase db;

public ScaledImageTarget(ImageEntity entity, int width, int height, String imageName, SQLiteDatabase db){
    this.entity = entity;
    this.width = width;
    this.height = height;
    isLoaded = false;
    this.imageName = imageName;
    this.db = db;
}

@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
    if (!isLoaded) {
        Bitmap resizedBitmap = Bitmap.createScaledBitmap(
                bitmap,
                width,
                height,
                false);
        bitmap.recycle();
        String filePath = saveToInternalSorage(resizedBitmap, imageName);
        resizedBitmap.recycle();
        ContentValues values = new ContentValues();
        values.put(PhotoContract.PhotoEntry.COLUMN_SCALED_PHOTO_URL, filePath);
        int result = db.update(
                PhotoContract.PhotoEntry.TABLE_NAME,
                values,
                "_id=?",
                new String[]{entity.getImageId()}
        );
        entity.getImageUrl();
    }
}

@Override
public void onBitmapFailed(Drawable drawable) {

}

@Override
public void onPrepareLoad(Drawable drawable) {
    if (entity.getScaledImageUrl() != null &&
            entity.getScaledImageUrl().length() > 0 &&
            new File(entity.getScaledImageUrl()).exists()) {
        isLoaded = true;
    }
}

private String saveToInternalSorage(Bitmap bitmapImage, String name){
    ContextWrapper cw = new ContextWrapper(getApplicationContext());
    // path to /data/data/yourapp/app_data/imageDir
    File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
    // Create imageDir
    File mypath=new File(directory, name);

    FileOutputStream fos = null;
    try {

        fos = new FileOutputStream(mypath);

        // Use the compress method on the BitMap object to write image to the OutputStream
        bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return mypath.getAbsolutePath();
}
}

运行此代码后,在db中,字段SCALED_PHOTO_URL仅针对第一个元素进行了更改,对于其他元素则为null。为什么会这样?

0 个答案:

没有答案