安卓:图像保存到的位置 [英] Android: Image save to location

查看:108
本文介绍了安卓:图像保存到的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在上显示一组图像,那么如果用户希望,有将图像保存到SD卡的能力。我需要帮助他们保存到外部存储。有人可以帮助我吗?

网格视图:

 公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.main);

GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);
gridview.setAdapter(新ImageAdapter(本));

gridview.setOnItemClickListener(新OnItemClickListener(){
    公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
        Toast.makeText(HelloGridView.this,+位置,Toast.LENGTH_SHORT).show();
    }
});
 

ImageAdapter:

 公共类ImageAdapter扩展了BaseAdapter {
私人语境mContext;

公共ImageAdapter(上下文C){
    mContext = C;
}

公众诠释getCount将(){
    返回mThumbIds.length;
}

公共对象的getItem(INT位置){
    返回null;
}

众长getItemId(INT位置){
    返回0;
}

//创建由适配器引用的每个项目的新的ImageView
公共查看getView(INT位置,查看convertView,ViewGroup中父){
    ImageView的ImageView的;
    如果(convertView == NULL){//如果它不回收,初始化一些属性
        ImageView的=新ImageView的(mContext);
        imageView.setLayoutParams(新GridView.LayoutParams(85,85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8,8,8,8);
    } 其他 {
        ImageView的=(ImageView的)convertView;
    }

    imageView.setImageResource(mThumbIds [位置]);
    返回ImageView的;
}

//引用到我们的形象
私人整数[] 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
};
 

解决方案

喜 我没有用过code我给你作为应用程序的一部分,但我没有使用这个在运行时在我的应用程序之一生成的调试位图。

 尝试{
           FileOutputStream中出=新的FileOutputStream(/ SD卡/ test2.png);
           mBitmap.com preSS(Bitmap.Com pressFormat.PNG,90,出);
        }赶上(例外五){
           e.printStackTrace();
        }
 

本code,只要你有一个位图,你只需要这个+清单的权限

 <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 

。希望做的伎俩为您

杰森

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 void 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

这篇关于安卓:图像保存到的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆