【问题标题】:why screenshots have low quality?为什么屏幕截图质量低?
【发布时间】:2014-04-02 15:16:07
【问题描述】:

我正在使用以下代码截取我的布局:

View u = findViewById(R.id.mainL);
u.setDrawingCacheEnabled(true);                                                
LinearLayout z = (LinearLayout) findViewById(R.id.mainL);
int totalHeight = z.getHeight();
int totalWidth = z.getWidth();
u.layout(0, 0, totalWidth, totalHeight);    
u.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(u.getDrawingCache());             
u.setDrawingCacheEnabled(false);

显然我没有在质量方面做任何事情,但结果位图(即 jpeg 图像)的质量非常差。

我做错了什么? 我觉得我应该以某种方式将其更改为 PNG 图像以获得更好的质量,但我不知道如何。 谢谢。

编辑:

对于那些问我在哪里保存位图的人,我不得不说我根本没有保存它。我只是用这段代码分享它:

        String pathofBmp = MediaStore.Images.Media.insertImage(getContentResolver(), b , "title", null);
        Uri bmpUri = Uri.parse(pathofBmp);
        Intent shareIntent = new Intent(Intent.ACTION_SEND);
        shareIntent.setType("image/jpeg");
        shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
        shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(Intent.createChooser(shareIntent, "Share"));

【问题讨论】:

  • “我做错了什么?” -- 如果没有别的,您没有显示保存 JPEG 文件的代码。 “我认为我应该以某种方式将其更改为 PNG 图像以获得更好的质量,但我不知道如何”——由于您没有提供保存 JPEG 文件的代码,我们无法建议您如何更改该代码用于保存 PNG 文件。
  • 看看我编辑的代码。

标签: android screenshot


【解决方案1】:

我不得不说我根本没有保存它

是的,你是。否则,您将没有pathofBmp。现在,在您的情况下,您将有关将位图保存到MediaStore 的所有事情都委派给您。我从未使用过insertImage(),也不知道它在文件格式、质量百分比等方面的作用。

如果您想更好地控制保存图像,use compress() on Bitmap 自己将其写入文件。

【讨论】:

  • 保存我的意思是图像将无法通过应用程序外部的图库使用。但是,是的,我必须使用该命令。谢谢。
【解决方案2】:

请试试这个:

View v = view.getRootView();
v.setDrawingCacheEnabled(true);
Bitmap b = v.getDrawingCache();
String extr = Environment.getExternalStorageDirectory().toString();
File myPath = new File(extr, getString(R.string.free_tiket)+".jpg");
FileOutputStream fos = null;
try {
    fos = new FileOutputStream(myPath);
    b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
    MediaStore.Images.Media.insertImage(getContentResolver(), b, "Screen", "screen");
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

我认为问题是 compress 100 可以解决您的问题。

reference

【讨论】:

  • 您的回答是以完全不同的方式完成我的工作。我只想创建一个位图用于共享而不是保存。
  • 你可以试试这个b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
猜你喜欢
  • 2020-11-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-06
  • 2021-03-23
  • 2014-10-11
  • 1970-01-01
相关资源
最近更新 更多