【发布时间】:2014-10-15 04:35:22
【问题描述】:
我想通过一个程序在我的 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>
【问题讨论】:
-
如果你要面对错误,我们为什么不呢?另外,您的意思是用单引号括住您的代码吗?
-
感谢分配 MEENAL SHARMA 和 JEREMY 不,我没有在我的程序中关闭单个代码中的代码。当我添加包名称 meri_application 时出现错误
-
你试过我提到的链接吗..
-
@Meenal Sharma 是的,我查看并想知道它会将图片保存在文件夹中,但我无法查看它并且不知道它保存屏幕截图的位置我找到了文件夹但没有图像是他们的
标签: android screenshot