0
나는이 코드를 작동시키는 방법과 메신저를 해답을 찾는 방법을 알아 내려고 노력하고 있습니다. 내 코드는 단순히 AOSP 소스에서 가져온 배경 화면 선택기입니다.갤러리 이미지 (월페이퍼)를 SD 카드에 저장 (휴관일)
Im 현재 배경 화면을 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();
}
을 추가하여 문제를 해결 한 이 코드를 읽으려면 .. – Maroun
@ Maroun Maroun 그래, 간단한 문제에 대한 코드의 그 많은, 내가 가진 주요 문제는 '메서드 getImage (int)는 단추에 대한 형식 갤러리에 대해 정의되지 않은'이해합니다. 문제는 문자열 s1 –
관련 코드 만 게시해야합니다. 도움이 될 것입니다. – Maroun