我正在开发一个Android项目,我想在其中拍摄当前活动的屏幕截图并将其保存为我的Android设备中的.jpeg文件。
当我使用FileOutputStream实例将位图图像流写入文件时,它会给我以下错误
参数3无法从'Java.IO.FileOutputStream'转换为'System.IO.Stream'
我的代码
private void ShareButton_Click(object sender, EventArgs e)
{
//create a bitmap screen capture
View screen = FindViewById(Resource.Layout.AboutImage);
Bitmap bitmap = Bitmap.CreateBitmap(screen.GetDrawingCache(false));
screen.SetWillNotCacheDrawing(true);
image = new File(directory, "Eco_Friendly " + mc.identifier);
FileOutputStream outputstream = new FileOutputStream(image);
int quality = 100;
bitmap.Compress(Bitmap.CompressFormat.Jpeg, quality, outputstream);//Here is error
}
问:如何解决这个问题?
答案 0 :(得分:1)
试试这段代码:
package com.screen.shots;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class CaptureScreenShots extends Activity {
LinearLayout L1;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen_shots);
L1 = (LinearLayout) findViewById(R.id.LinearLayout01);
Button but = (Button) findViewById(R.id.munchscreen);
but.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
View v1 = L1.getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bm = v1.getDrawingCache();
BitmapDrawable bitmapDrawable = new BitmapDrawable(bm);
image = (ImageView) findViewById(R.id.screenshots);
image.setBackgroundDrawable(bitmapDrawable);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.screen_shots, menu);
return true;
}
}
结果:
答案 1 :(得分:0)
View root = getWindow().getDecorView().getRootView();
root.setDrawingCacheEnabled(true);
root.buildDrawingCache();
Bitmap snapshot = root.getDrawingCache();
最后,你应该打电话给这个
root.destroyDrawingCache();