我正在开发一个应用程序,其中有3个按钮。 2个按钮用于更改图像,第3个按钮用于将当前图像保存在我们的设备中。我不知道如何使用该代码。所以PLZ帮我代码。这是我的代码。
主要的java
Public class MainActivity extends Activity implements OnClickListener {
int pos=0;
int amount=10;
int max=amount -1;
int min=0;
Button prev, next, save;
ImageView image;
Bitmap bitmap;
final int [] imgs = new int [] {R.drawable.a1, R.drawable.a2, R.drawable.a3, //so on to a10};
@Override
Public void onCreat (Bundle savedInstance){
super.onCreat (savedInstanceState);
setContentView (R.layout.activity_main);
image=(ImageView) findViewById (R.id.ImageViewPic);
prev=(Button) findViewById (R.id.bprev);
next=(Button) findViewById (R.id.bnext);
save=(Button) findViewById (R.id.bsave);
image.setImageResource (R.drawable.a1);
prev.setOnClickListener (this);
next.setOnClickListener (this);
save.setOnClickListener (this);
}
@Override
public void onClick (View arg0) {
switch (arg0.getId ()){
case R.id.bprev:
if (pos > min) {
pos--;
image.setImageResource (imgs [pos]);
;
} else {
}
break;
case R.id.bnext:
if (pos < max) {
pos++;
image.setImageResource (imgs [pos]);
;
} else {
}
break;
case R.id.bsave:
// So here is the main thing. Which code I have to write here. Plz guide me.
答案 0 :(得分:0)
使用此方法保存ImageView
...
private void saveImage(ImageView imageView) {
Drawable image = imageView.getDrawable();
if(image != null && image instanceof BitmapDrawable) {
BitmapDrawable drawable = (BitmapDrawable) image;
Bitmap bitmap = drawable.getBitmap();
try {
File file = new File("path where you want to save");
FileOutputStream stream = new FileOutputStream(file);
bitmap.compress(CompressFormat.PNG, 100, stream);
stream.flush();
stream.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}