开发者

Android: Image save to location

开发者 https://www.devze.com 2023-01-18 22:42 出处:网络
I\'m working on displaying a set of images, then if the users wishes, to have the ability to save the image to the SD card. I need help saving them to external storage. Can someone help me out with th

I'm working on displaying a set of images, then if the users wishes, to have the ability to save the image to the SD card. I need help saving them to external storage. Can someone help me out with this?

Grid View:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));

gridview.setOnItemClickListener(new OnItemClickListener() {
    public vo开发者_开发知识库id onItemClick(AdapterView<?> parent, View v, int position, long id) {
        Toast.makeText(HelloGridView.this, "" + position, Toast.LENGTH_SHORT).show();
    }
});

ImageAdapter:

public class ImageAdapter extends BaseAdapter {
private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

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

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

// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) {  // if it's not recycled, initialize some attributes
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(mThumbIds[position]);
    return imageView;
}

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1,
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7
};


Hi I haven't used the code I'm giving you as part of an application but I did use this to debug bitmaps generated at runtime in one of my apps.

try {
           FileOutputStream out = new FileOutputStream("/sdcard/test2.png");
           mBitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
        } catch (Exception e) {
           e.printStackTrace();
        }

with this code as long as you have a bitmap, you just need this + the manifest permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Hope that does the trick for you

Jason

0

精彩评论

暂无评论...
验证码 换一张
取 消