【问题标题】:screenshots in android programmatically以编程方式在android中的屏幕截图
【发布时间】: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


【解决方案1】:

只需用这个替换保存位图方法

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);
    }
}

【讨论】:

  • 仍然找不到截图图片
  • 您已经实现了与我提到的链接中相同的代码,因为我已经尝试过它并且它正在工作,只是对保存位图方法进行了一些更改。
  • 你发送它我会在我身边检查这里
  • 你的邮箱是什么,我可以给你发邮件
  • 你能把你的代码粘贴到这里吗..只需编辑你的问题并将你的代码粘贴到那里..
【解决方案2】:

你可以试试这个:

// 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);

最后保存并压缩图像:

try {
    fout = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
    fout.flush();
    fout.close();

}

【讨论】:

  • 我已经看过这个,但我无法理解它有点复杂,或者我可能没有正确理解它,但不知何故这就是问题
  • 请看我的解释...我想这会对你有所帮助。
  • 请告诉我屏幕截图将保存在哪里以及我可以在哪里找到我的文件夹。我怎么知道屏幕截图被捕获了
  • 检查到您的设备根目录。 Environment.getExternalStorageDirectory().getPath() 返回设备根目录(外部 SD 卡)。
  • 仍然找不到截图图片
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-15
  • 1970-01-01
相关资源
最近更新 更多