裁剪所选图像保存在可绘制文件夹中

时间:2013-06-29 05:37:32

标签: android image android-drawable

我在drawable文件夹中保存了一些图像&将它们保存到一个数组中。然后我在网格视图列表中显示这些图像,&在项目单击方法我想裁剪所选图像。当我选择项目是选择但是活动结果方法给出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);
         */
    }

}

0 个答案:

没有答案
相关问题