0

我试图弄清楚如何使这段代码工作并且我失去了答案。我的代码只是一个墙纸选择器,是从 AOSP 源代码中提取的。

我添加了一个按钮,将当前壁纸保存到 SD 卡。我有以下代码,不知道如何解决这个问题。提前致谢

View setButton2 = view.findViewById(R.id.savetosd);
    setButton2.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            int i = gallery.getSelectedItemPosition();
            String s = (new StringBuilder(String.valueOf(getResources().getStringArray(R.array.wallpapers)[i]))).append(".jpg").toString();
            String s1 (new saveToSDCard(.getImage(gallery.getSelectedItemPosition()), s);
            startMediaScanner(s1);
            Toast.makeText(getActivity().getApplicationContext(), "Saved to SD Card", Toast.LENGTH_SHORT).show();

当前误差

The method getImage(int) is undefined for the type Gallery

其余代码

private static final String SD = Environment.getExternalStorageDirectory().getAbsolutePath();
private boolean mEmbedded;
private Bitmap mBitmap = null;
private ArrayList<Integer> mThumbs;
private ArrayList<Integer> mImages;
private WallpaperLoader mLoader;
private WallpaperDrawable mWallpaperDrawable = new WallpaperDrawable();
...

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    findWallpapers();

    if (mEmbedded) {
        View view = inflater.inflate(R.layout.wallpaper_chooser, container, false);
        view.setBackground(mWallpaperDrawable);

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

        View setButton = view.findViewById(R.id.set);
        setButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                selectWallpaper(gallery.getSelectedItemPosition());
            }
        });
        View setButton2 = view.findViewById(R.id.savetosd);
        setButton2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //selectWallpaper(gallery.getSelectedItemPosition());
                int i = gallery.getSelectedItemPosition();
                String s = (new StringBuilder(String.valueOf(getResources().getStringArray(R.array.wallpapers)[i]))).append(".jpg").toString();
                String s1 (new saveToSDCard(.getImage(gallery.getSelectedItemPosition()), s);
                startMediaScanner(s1);
                Toast.makeText(getActivity().getApplicationContext(), "Saved to SD Card", Toast.LENGTH_SHORT).show();

            }
        });
        return view;
    }
    return null;
}

private void selectWallpaper(int position) {
    try {
        WallpaperManager wpm = (WallpaperManager) getActivity().getSystemService(
                Context.WALLPAPER_SERVICE);
        wpm.setResource(mImages.get(position));
        Activity activity = getActivity();
        activity.setResult(Activity.RESULT_OK);
        activity.finish();
    } catch (IOException e) {
        Log.e(TAG, "Failed to set wallpaper: " + e);
    }
}

public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    selectWallpaper(position);
}

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

public void onNothingSelected(AdapterView<?> parent) {
}

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 + "_thumb",
                    "drawable", packageName);

            if (thumbRes != 0) {
                mThumbs.add(thumbRes);
                mImages.add(res);
                // Log.d(TAG, "add: [" + packageName + "]: " + extra + " (" + res + ")");
            }
        }
    }    
}

private class ImageAdapter extends BaseAdapter implements ListAdapter, SpinnerAdapter {
    private LayoutInflater mLayoutInflater;

    ImageAdapter(Activity activity) {
        mLayoutInflater = activity.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) {
        View view;

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

        ImageView image = (ImageView) view.findViewById(R.id.wallpaper_image);

        int thumbRes = mThumbs.get(position);
        image.setImageResource(thumbRes);
        Drawable thumbDrawable = image.getDrawable();
        if (thumbDrawable != null) {
            thumbDrawable.setDither(true);
        } else {
            Log.e(TAG, "Error decoding thumbnail resId=" + thumbRes + " for wallpaper #"
                    + position);
        }
        return view;
    }
}

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

    WallpaperLoader() {
        mOptions = new BitmapFactory.Options();
        mOptions.inDither = false;
        mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
    }
    @Override
    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();
            }

            View v = getView();
            if (v != null) {
                mBitmap = b;
                mWallpaperDrawable.setBitmap(b);
                v.postInvalidate();
            } else {
                mBitmap = null;
                mWallpaperDrawable.setBitmap(null);
            }
            mLoader = null;
        } else {
           b.recycle();
        }
    }
    void cancel() {
        mOptions.requestCancelDecode();
        super.cancel(true);
    }
}
 private String saveToSDCard(Bitmap bitmap, String s)
 {
    StringBuffer stringbuffer = new StringBuffer();
    File file = new File ((new StringBuilder(String.valueOf(SD))).append("/JaisonBrooks|Development/GalaxyS3Wallpapers/").toString());
    if(!file.exists())
        file.mkdirs();
    File file1 = new File(file, s);
    if(file1.exists())
        file1.delete();
    ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
    bitmap.compress(android.graphics.Bitmap.CompressFormat.JPEG, 100, bytearrayoutputstream);
    byte abtye0[] = bytearrayoutputstream.toByteArray();
    try { 
        file1.createNewFile();
        FileOutputStream fileoutputstream = new FileOutputStream(file1);
        fileoutputstream.write(abtye0);
        fileoutputstream.flush();
        fileoutputstream.close();
        stringbuffer.append(file1.getAbsolutePath());
    }
    catch (IOException ioexception)
    {
        ioexception.printStackTrace();
    }
    return stringbuffer.toString();

    }
4

1 回答 1

0

我已经通过添加以下代码解决了这个问题

 private ImageAdapter mAdapter;

 final Gallery gallery = (Gallery) view.findViewById(R.id.gallery);
        gallery.setCallbackDuringFling(false);
        gallery.setOnItemSelectedListener(this);
        //gallery.setAdapter(new ImageAdapter(getActivity()));
        mAdapter = new ImageAdapter(getActivity());
        gallery.setAdapter(mAdapter);

        View setButton = view.findViewById(R.id.set);
        setButton.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                selectWallpaper(gallery.getSelectedItemPosition());
            }
        });
        View setButton2 = view.findViewById(R.id.savetosd);
        setButton2.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                //selectWallpaper(gallery.getSelectedItemPosition());
                int i = gallery.getSelectedItemPosition();
                String s = (new StringBuilder(String.valueOf(getResources().getStringArray(R.array.wallpapers)[i]))).append(".jpg").toString();
                String s1 = saveToSDCard(mAdapter.getImage(gallery.getSelectedItemPosition()), s);
                startMediaScanner(s1);
                Toast.makeText(getActivity().getApplicationContext(), "Saved to SD Card", Toast.LENGTH_SHORT).show();

            }
        });

   ...

   public Bitmap getImage(int i)
    {
        return getBitmap(((Integer)mImages.get(i)).intValue());
    }

    public Bitmap getItem(int i)
    {
        return getBitmap(((Integer)mImages.get(i)).intValue());
    }
于 2013-03-18T20:09:17.520 回答