描画可能なフォルダーにいくつかの画像を保存し、それらを配列に保存します。次に、これらの画像をグリッド ビュー リストに表示し、アイテム クリック メソッドで選択した画像をトリミングします。アイテムを選択すると選択されますが、アクティビティの結果メソッドでは null が返されます。しかし、ギャラリーの場合、簡単に画像をトリミングできます。以下は私が試したコードです。
コード:
public class SetWallpaper_Inbuilt extends Activity {
Integer[] img = { R.drawable.dew_drops, R.drawable.droplets,
R.drawable.eiffel_tower, R.drawable.whitetrees, R.drawable.windy,
R.drawable.cold, R.drawable.thundersnow, R.drawable.snowstorms,
R.drawable.hot };
WallpaperManager wpm;
GridView gridview;
// for cropping
final int CAMERA_CAPTURE = 1;
final int PIC_CROP = 1;
private Uri picUri;
Context context;
File f;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setwallpaper_inbuilt);
gridview = (GridView) findViewById(R.id.gridview);
final ImageAdapter IA = new ImageAdapter(this, img);
gridview.setAdapter(IA);
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
try{
Uri y = Uri.parse("com.NM.quote_db/drawable/"+R.drawable.cloudy1);
//Intent intent = new Intent();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(y, "image/*");
// intent.setData(y);
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 80);
intent.putExtra("outputY", 80);
intent.putExtra("return-data", true);
startActivityForResult(intent, PIC_CROP);
Toast.makeText(getApplicationContext(), "Activity found", Toast.LENGTH_SHORT).show();
startActivityForResult(intent, 1);*/
}
catch(ActivityNotFoundException e)
{
Toast.makeText(getApplicationContext(), "Activity not found", Toast.LENGTH_SHORT).show();
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try
{
if (resultCode == RESULT_OK || resultCode == RESULT_CANCELED) {
if(requestCode == PIC_CROP){
//get the returned data
Bundle extras = data.getExtras();
//get the cropped bitmap
Bitmap thePic = extras.getParcelable("data");
wpm = WallpaperManager.getInstance(getApplicationContext());
try {
wpm.setBitmap(thePic);
Toast.makeText(getApplicationContext(), "wallpaper set", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else
Toast.makeText(getApplicationContext(), "Request code does not match", Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(getApplicationContext(), "result code does not match", Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), "result code is"+ Integer.toString(resultCode), Toast.LENGTH_SHORT).show();
}
catch(Exception e)
{
Toast.makeText(getApplicationContext(), "outside start activity result method", Toast.LENGTH_SHORT).show();
}
}}
イメージ アダプター クラス
public class ImageAdapter extends BaseAdapter {
Context ImageGallaryContext;
Integer[] ImgId;
private Bitmap[] bitmap;
public ImageAdapter(Context c, Integer[] ImgId) {
ImageGallaryContext = c;
this.ImgId = ImgId;
bitmap = new Bitmap[getCount()];
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return ImgId.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
bitmap[position] = BitmapFactory.decodeResource(ImageGallaryContext.getResources(), ImgId[position]);
return bitmap[position];
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView iView;
if (convertView == null) {
iView = new ImageView(ImageGallaryContext);
iView.setLayoutParams(new GridView.LayoutParams(185, 185));
iView.setScaleType(ImageView.ScaleType.CENTER_CROP);
iView.setPadding(5, 5, 5, 5);
} else {
iView = (ImageView) convertView;
}
iView.setImageResource(ImgId[position]);
return iView;
/*
* Intent i = new Intent(this, Wallpaper.class); startActivity(i);
*/
}
}