我想通过程序在我的android活动中截取屏幕截图,以便当用户点击按钮截取屏幕截图时,屏幕截图应保存在SD卡中。我正在添加以下代码并面临一些错误。在eclipse中,代码中的包名称用红线TOSTRING(com.meri.meri_application)加下划线。
public class PartySalesandRecovery extends Activity implements OnClickListener{
UserFunctions userFunction;
DatabaseHandler db;
String str;
String moneyString2,moneyString3,moneyString4,moneyString5,moneyString6,moneyString7;
String month,loginName,week;
ImageView imageView;
Bitmap bmp;
int orientation;
int total_sale_trg = 0;
int total_sale_act = 0;
int total_rec_trg = 0;
int total_rec_act = 0;
int total_per_sale = 0;
int total_per_rec = 0;
private static String KEY_SUCCESS = "success";
TableLayout tl;
LayoutParams lp;
Spinner quesWeek,quesMonth;
Button btnShow,btnTakeScreen;
JSONObject json,json_user;
private View view;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.partysalesandrecovery);
view= findViewById(R.id.screenRoot);
orientation = this.getResources().getConfiguration().orientation;
Calendar cal=Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMMMMMMMM");
String month_name = month_date.format(cal.getTime());
quesMonth = (Spinner)findViewById(R.id.comboMonth);
quesWeek = (Spinner)findViewById(R.id.comboWeek);
ArrayAdapter myAdap = (ArrayAdapter) quesMonth.getAdapter();
int spinnerPosition = myAdap.getPosition(month_name);
quesMonth.setSelection(spinnerPosition);
tl = (TableLayout)findViewById(R.id.tableLayoutLocationSaleandRecovery);
btnShow = (Button)findViewById(R.id.btnShowReports);
btnShow.setOnClickListener(this);
btnTakeScreen = (Button)findViewById(R.id.btnTakeScreenShot);
btnTakeScreen.setOnClickListener(this);
}
///////////////////////////////
///////////////////////////////////////
public void saveBitmap(Bitmap bitmap) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
}catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
////////////////////////////////
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(v.getId()==R.id.btnTakeScreenShot)
{
Bitmap bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ALPHA_8);
View from = (LinearLayout)findViewById(R.id.viewRoot);
Bitmap result = drawViewToBitmap(from, bitmap);
imageView = (ImageView)findViewById(R.id.imgViewpic);
imageView.setImageBitmap(result);
saveBitmap(bitmap);
try
{
imageView.setDrawingCacheEnabled(true);
bmp = Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, e.getMessage(), 7000).show();
}
}
code of XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/viewRoot" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/comboMonth"
android:prompt="@string/month_prompt"
android:entries="@array/question_months"
android:layout_weight="0.5"
/>
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/comboWeek"
android:entries="@array/question_weeks"
android:prompt="@string/week_prompt"
android:layout_weight="0.5"
/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Show Reports"
android:id="@+id/btnShowReports"
/>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#000000"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Projects Sales and Recovery Status"
android:textSize="16sp"
android:textStyle="bold"
android:gravity="center"
/>
<TableLayout
android:id="@+id/tableLayoutLocationSaleandRecovery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:shrinkColumns="*"
android:stretchColumns="*" >
</TableLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture"
android:layout_gravity="right"
android:id="@+id/btnTakeScreenShot"
/>
<ImageView
android:id="@+id/imgViewpic"
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/ic_launcher"
android:layout_gravity="center"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
答案 0 :(得分:0)
你可以试试这个:
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + ACCUWX.IMAGE_APPEND;
// create bitmap screen capture
Bitmap bitmap;
View v1 = getWindow().getDecorView().getRootView(); // View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
OutputStream fout = null;
imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这个答案来自于此: How to programmatically take a screenshot in Android?
说明: 输出图像路径,图像名称为:
String mPath = Environment.getExternalStorageDirectory().getPath() + "/" + "ScreenShot.jpg";
获取当前视图/显示的屏幕图像并从中创建位图:
Bitmap bitmap;
View v1 = getWindow().getDecorView().getRootView(); // View v1 = mCurrentUrlMask.getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
最后保存&amp;压缩图像:
try {
fout = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
}
答案 1 :(得分:0)
只需用此
替换保存位图方法public void saveBitmap(Bitmap bitmap) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
编辑1:
只需使用此代码重新编写代码
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.btnTakeScreenShot) {
Bitmap bitmap = takeScreenshot()
imageView.setImageBitmap(bitmap);
saveBitmap(bitmap);
}
}
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "test.jpg");
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}