在Wallpaper Chooser中实现扩展文件

时间:2014-07-05 17:31:59

标签: android image-gallery apk-expansion-files android-wallpaper

我在Google Play上有一个应用程序包含壁纸(它是壁纸选择器)仍然使用图库(http://developer.android.com/reference/android/widget/Gallery.html,是的,它是一个旧的应用程序)。我正在尝试实现apk扩展文件以打破50MB的限制。

现在,应用程序下载程序和GP的.obb下载效果非常好。我可以在/ Android / obb中找到它。我还是无法从.obb文件中准备壁纸。我试过APK Expansion Zip Library,但我不知道怎么用它和Gallery ...

这是我的wallpaper.java文件:

public class wallpaper extends Activity implements AdapterView.OnItemSelectedListener,
    OnClickListener {

private Gallery mGallery;
private ImageView mImageView;
private TextView mInfoView;
private boolean mIsWallpaperSet;

private Bitmap mBitmap;

private ArrayList<Integer> mThumbs;
private ArrayList<Integer> mImages;
private WallpaperLoader mLoader;

@Override
public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    // Start wallpaper app
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    findWallpapers();

    setContentView(R.layout.wallpaper_chooser);

    mGallery = (Gallery) findViewById(R.id.gallery);
    mGallery.setAdapter(new ImageAdapter(this));
    mGallery.setOnItemSelectedListener(this);
    mGallery.setCallbackDuringFling(false);

    findViewById(R.id.set).setOnClickListener(this);

    mImageView = (ImageView) findViewById(R.id.wallpaper);
    mInfoView = (TextView) findViewById(R.id.info);
}

private void findWallpapers() {
    mThumbs = new ArrayList<Integer>(24);
    mImages = new ArrayList<Integer>(24);

    final Resources resources = getResources();
    final String packageName = getApplication().getPackageName();

    addWallpapers(resources, packageName, R.array.wallpapers);
    addWallpapers(resources, packageName, R.array.extra_wallpapers);
}

private void addWallpapers(Resources resources, String packageName, int list) {
    final String[] extras = resources.getStringArray(list);
    for (String extra : extras) {
        int res = resources.getIdentifier(extra, "drawable", packageName);
        if (res != 0) {
            final int thumbRes = resources.getIdentifier(extra + "_small",
                    "drawable", packageName);

            if (thumbRes != 0) {
                mThumbs.add(thumbRes);
                mImages.add(res);
            }
        }
    }
}

@Override
protected void onResume() {
    super.onResume();
    mIsWallpaperSet = false;
}

@Override
protected void onDestroy() {
    super.onDestroy();

    if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
        mLoader.cancel(true);
        mLoader = null;
    }
}

public void onItemSelected(AdapterView parent, View v, int position, long id) {
    if (mLoader != null && mLoader.getStatus() != WallpaperLoader.Status.FINISHED) {
        mLoader.cancel();
    }
    mLoader = (WallpaperLoader) new WallpaperLoader().execute(position);
}

/*
 * When using touch if you tap an image it triggers both the onItemClick and
 * the onTouchEvent causing the wallpaper to be set twice. Ensure we only
 * set the wallpaper once.
 */
private void selectWallpaper(int position) {
    if (mIsWallpaperSet) {
        return;
    }

    mIsWallpaperSet = true;
    try {
        InputStream stream = getResources().openRawResource(mImages.get(position));
        setWallpaper(stream);
        setResult(RESULT_OK);
        finish();
    } catch (IOException e) {
        Log.e("Paperless System", "Failed to set wallpaper: " + e);
    }
}

public void onNothingSelected(AdapterView parent) {
}

private class ImageAdapter extends BaseAdapter {
    private LayoutInflater mLayoutInflater;

    ImageAdapter(wallpaper context) {
        mLayoutInflater = context.getLayoutInflater();
    }

    public int getCount() {
        return mThumbs.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView image;

        if (convertView == null) {
            image = (ImageView) mLayoutInflater.inflate(R.layout.wallpaper_item, parent, false);
        } else {
            image = (ImageView) convertView;
        }

        int thumbRes = mThumbs.get(position);
        image.setImageResource(thumbRes);
        Drawable thumbDrawable = image.getDrawable();
        if (thumbDrawable != null) {
            thumbDrawable.setDither(true);
        } else {
            Log.e("Paperless System", String.format(
                "Error decoding thumbnail resId=%d for wallpaper #%d",
                thumbRes, position));
        }
        return image;
    }
}

public void onClick(View v) {
    selectWallpaper(mGallery.getSelectedItemPosition());
}

class WallpaperLoader extends AsyncTask<Integer, Void, Bitmap> {
    BitmapFactory.Options mOptions;

    WallpaperLoader() {
        mOptions = new BitmapFactory.Options();
        mOptions.inDither = false;
        mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;            
    }

    protected Bitmap doInBackground(Integer... params) {
        if (isCancelled()) return null;
        try {
            return BitmapFactory.decodeResource(getResources(),
                    mImages.get(params[0]), mOptions);
        } catch (OutOfMemoryError e) {
            return null;
        }            
    }

    @Override
    protected void onPostExecute(Bitmap b) {
        if (b == null) return;

        if (!isCancelled() && !mOptions.mCancel) {
            // Help the GC
            if (mBitmap != null) {
                mBitmap.recycle();
            }

            mInfoView.setText(getResources().getStringArray(R.array.info)[mGallery.getSelectedItemPosition()]);

            final ImageView view = mImageView;
            view.setImageBitmap(b);

            mBitmap = b;

            final Drawable drawable = view.getDrawable();
            drawable.setFilterBitmap(true);
            drawable.setDither(true);

            view.postInvalidate();

            mLoader = null;
        } else {
           b.recycle(); 
        }
    }

    void cancel() {
        mOptions.requestCancelDecode();
        super.cancel(true);
    }
}

壁纸在res / values / wallpapers.xml中定义:

<resources>
<string-array name="wallpapers" translatable="false">
    <item>wallpaper1</item>
    <item>wallpaper2</item>
    <item>wallpaper3</item>
    [...]
    </string-array>

<string-array name="info" translatable="false">
    <item>wallpaper1 description</item>
    <item>wallpaper2 description</item>
    <item>wallpaper3 description</item>
    [...]
</string-array>

如何将我的壁纸补丁从/ res / drawable更改为/obb/com.example.app/main.8.com.example.app.obb?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

首先阅读有关APK扩展文件的信息:http://developer.android.com/google/play/expansion-files.html 然后,在此处详细了解使用图库窗口小部件:http://developer.android.com/reference/android/widget/Gallery.html

最后,考虑使用从此处引用的ViewPager:http://developer.android.com/training/implementing-navigation/lateral.html

我不确定你是否应该提出这样的问题,如果我错了,请纠正我。