Android 将图像从抽屉保存到sd卡
我需要帮助将图像从可拔取设备保存到sd卡。当我单击“保存”按钮时,它仅从图像视图将可拔取设备a1保存到sd卡。但我想保存当前预览的图像,该图像不可绘制。这是我的主要活动Android 将图像从抽屉保存到sd卡,android,eclipse,bitmap,imageview,Android,Eclipse,Bitmap,Imageview,我需要帮助将图像从可拔取设备保存到sd卡。当我单击“保存”按钮时,它仅从图像视图将可拔取设备a1保存到sd卡。但我想保存当前预览的图像,该图像不可绘制。这是我的主要活动 public class MainActivity extends Activity { private static final List<Integer> backgrounds = new ArrayList<Integer>(); private static final int
public class MainActivity extends Activity {
private static final List<Integer> backgrounds = new ArrayList<Integer>();
private static final int TOTAL_IMAGES;
static {
backgrounds.add(R.drawable.a1);
backgrounds.add(R.drawable.a2);
backgrounds.add(R.drawable.a3);
backgrounds.add(R.drawable.a4);
backgrounds.add(R.drawable.a5);
backgrounds.add(R.drawable.a6);
backgrounds.add(R.drawable.a7);
backgrounds.add(R.drawable.a8);
TOTAL_IMAGES = (backgrounds.size() - 1);
}
private int currentPosition = 0;
private ImageView backgroundPreview;
Bitmap bitmap;
OutputStream output;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
backgroundPreview = (ImageView) findViewById(R.id.backgroundPreview);
// Set the default image to be shown to start with
changePreviewImage(currentPosition);
}
public void gotoPreviousImage(View v) {
int positionToMoveTo = currentPosition;
positionToMoveTo--;
if(positionToMoveTo < 0){
positionToMoveTo = TOTAL_IMAGES;
}
changePreviewImage(positionToMoveTo);
}
public void save(View v) {
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a1);
File filepath = Environment.getExternalStorageDirectory();
File dir = new File(filepath.getAbsolutePath()
+ "/folder/folder/");
dir.mkdirs();
File file = new File(dir, "Image1.png");
Toast.makeText(MainActivity.this, "Image Saved to SD Card",
Toast.LENGTH_SHORT).show();
try {
output = new FileOutputStream(file);
// Compress into png format image from 0% - 100%
bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void gotoNextImage(View v) {
int positionToMoveTo = currentPosition;
positionToMoveTo++;
if(currentPosition == TOTAL_IMAGES){
positionToMoveTo = 0;
}
changePreviewImage(positionToMoveTo);
}
public void changePreviewImage(int pos) {
currentPosition = pos;
backgroundPreview.setImageResource(backgrounds.get(pos));
Log.d("Main", "Current position: "+pos);
}
};
公共类MainActivity扩展活动{
私有静态最终列表背景=新ArrayList();
私有静态最终整型总图像;
静止的{
背景。添加(R.drawable.a1);
背景。添加(R.drawable.a2);
背景。添加(R.drawable.a3);
背景。添加(R.drawable.a4);
背景。添加(R.drawable.a5);
背景。添加(R.drawable.a6);
背景。添加(R.drawable.a7);
背景。添加(R.drawable.a8);
图像总数=(backgrounds.size()-1);
}
私有int currentPosition=0;
私有ImageView背景预览;
位图;
输出流输出;
/**在首次创建活动时调用*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
backgroundPreview=(ImageView)findViewById(R.id.backgroundPreview);
//将要显示的默认图像设置为以开始
更改预览图像(当前位置);
}
public void gotoPreviousImage(视图v){
int POSITIONTOMOVIOVE=当前位置;
否决权--;
如果(位置小于0){
POSITIONTOMOVIOVE=总图像;
}
变更预览图像(位置否决);
}
公共作废保存(视图v){
位图=BitmapFactory.decodeResource(getResources(),R.drawable.a1);
文件filepath=Environment.getExternalStorageDirectory();
File dir=新文件(filepath.getAbsolutePath()
+“/folder/folder/”;
dir.mkdirs();
File File=新文件(dir,“Image1.png”);
Toast.makeText(MainActivity.this,“图像保存到SD卡”,
吐司。长度(短)。show();
试一试{
输出=新文件输出流(文件);
//从0%-100%压缩为png格式图像
compress(bitmap.CompressFormat.PNG,100,输出);
output.flush();
output.close();
}
捕获(例外e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
}
public void gotoNextImage(视图五){
int POSITIONTOMOVIOVE=当前位置;
位置否决++;
如果(当前位置==总图像){
位置否决权=0;
}
变更预览图像(位置否决);
}
公共无效更改预览图像(int pos){
当前位置=位置;
backgroundPreview.setImageResource(backgrounds.get(pos));
日志d(“主”,“当前位置:+pos”);
}
};
在保存的第一行:
bitmap = BitmapFactory.decodeResource(getResources(), backgrounds.get(currentPosition)) ;