如何在Java中本地保存(屏幕截图)?

时间:2015-04-16 18:50:02

标签: java android file nexus-7

我即将结束应用程序开发的实习,我被分配从头开始创建一个应用程序,而不是移植苹果版本。我无法弄清楚如何保存应用程序中的屏幕截图,然后将其保存到设备中。我不想在外部保存,因为你不能假设每个人都使用SD卡。

这是我的代码,当我点击发送时,我想要生成并保存截图,这样当我使用我的分享意图时,我可以发送屏幕截图。

import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.ShareActionProvider;
import android.support.v4.view.MenuItemCompat;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Toast;
import android.text.TextWatcher;
import android.text.Editable;
import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.content.Intent;
import android.content.Context;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends ActionBarActivity {
TextView CodexTV;
EditText CodexET;

@Override
public class MainActivity extends ActionBarActivity {
TextView CodexTV;
EditText CodexET;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //Loading our AvianKingdom Codex Font
    Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/WWAvianKingdom-Regular.ttf");
    //Text view label
    CodexET = ((EditText) findViewById(R.id.CodexMessage));
    //REAL-TIME Textview change
    CodexET.addTextChangedListener(new TextWatcher() {
        public void afterTextChanged(Editable s) {   //Convert the Text to String
            String inputText = CodexET.getText().toString();
            CodexTV = ((TextView) findViewById(R.id.CustomFontText));
            CodexTV.setText(inputText);
        }
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            // TODO Auto-generated method stub
        }
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub
        }
    });

    //Applying the font
    CodexET.setTypeface(tf);

    //Screenshot our Codex'ed Image
    CodexET.setOnKeyListener(new OnKeyListener()
    {
        public boolean onKey(View v, int keyCode, KeyEvent event)
        {
            if (event.getAction() == KeyEvent.ACTION_DOWN)
            {
                switch (keyCode)
                {
                    case KeyEvent.KEYCODE_DPAD_CENTER:
                        doShare(getDefaultIntent());
                        captureScreen();
                    case KeyEvent.KEYCODE_ENTER:
                        doShare(getDefaultIntent());
                        captureScreen();
                    default:
                        break;
                }
            }
            return false;
        }
    });
}

// Basic Share Intent to handle Sharing through Applications.
private Intent getDefaultIntent()
{
    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("image/*");
    return intent;
}

private ShareActionProvider mShareActionProvider;

以下是用户点击发送时调用的函数:

 //Function for Screenshot and Saving picture.
public Bitmap captureScreen()
{   // Image naming and path.
    String fileName = "DR-Codex.jpg";

    // create bitmap screen capture
    Bitmap bitmap;
    View v1 = CodexET.getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);
    //Start writing to device.
    OutputStream fout = null;
    File imageFile;
    imageFile = new File(getExternalFilesDir(null), fileName);
    try
    {
        //Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);
        //intent.setDataAndType(Uri.fromFile(new File(fileName)), "image/*");
        //startActivity(intent);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        bitmap.compress(CompressFormat.JPEG, 90, bos);
        byte[] bitmapdata = bos.toByteArray();

        fout = new FileOutputStream(imageFile);
        fout.write(bitmapdata);
        fout.flush();
        Log.e("File saved yo", "but where");
        fout.close();
    }
    catch (FileNotFoundException e)
    {
        // TODO Auto-generated catch block
        Log.e("File Selector", "The selected file can't be shared: " + fileName);
        e.printStackTrace();
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        Log.e("File Selector", "The selected file can't be shared: " + fileName);
        e.printStackTrace();
    }
    finally {
        //Stuff
    }
    return bitmap;
}


private void doShare(Intent shareintent)
{
    mShareActionProvider.setShareIntent(shareintent);
    invalidateOptionsMenu();
}

似乎使用Action.Create_Document意图我可以创建一个空白文件,但仍然没有生成我的屏幕图像。

这是应用程序的屏幕看起来像。 Screenshot of App

这是最新的报告清单,我相信我已经突出了这个问题。 DDMS Report

1 个答案:

答案 0 :(得分:2)

两件事对我来说很突出......

1)将.jpg扩展名添加到您的文件名中。即“DR-CODEX.jpg”

2)不要保存到私有内部getFilesDir,因为安全性会导致共享问题。而是保存到getExternalFilesDir(null)

修改

也因为getExternalFilesDir,你需要AndroidManifest.xml中的以下内容

   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

可能

   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />