2010-10-02 99 views
3

我正在顯示一組圖像,然後如果用戶希望,有能力將圖像保存到SD卡。我需要幫助將它們保存到外部存儲。有人可以幫我解決這個問題嗎?Android:圖像保存到位置

網格視圖:

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 
}; 

回答

3

嗨 我沒有用我給你的代碼作爲應用程序的一部分,但我沒有使用這是爲了調試我的一個應用程序在運行時生成的位圖。

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

用,只要你有一個位圖的代碼,你只需要這+清單許可

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

。希望做的伎倆爲您

傑森