gpt4 book ai didi

java - 如何在 Java 中本地保存文件(截图)?

转载 作者:行者123 更新时间:2023-11-29 20:48:57 24 4
gpt4 key购买 nike

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

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

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 Intent 我可以创建一个空白文件,但我的屏幕图像仍未生成。

这是应用程序屏幕的样子。 Screenshot of App

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

最佳答案

有两件事让我印象深刻......

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

2) 不要保存到私有(private)内部 getFilesDir,因为那样您将因安全问题而无法共享。而是保存到 getExternalFilesDir(null)

编辑

同样由于 getExternalFilesDir,您将需要在 AndroidManifest.xml 中添加以下内容

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

可能

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

关于java - 如何在 Java 中本地保存文件(截图)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29683504/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com