拿屏幕并保存android

我是一个非常新的Android开发人员,对java有基本的了解。 我承担了一个大型项目,我遇到了一个似乎无法修复的问题。

基本上我的问题有三个不同的部分:

  1. 我需要截取我的布局截图。 我不能只使用内置的截图function,它需要在按下按钮时完成。

  2. 然后我需要裁剪图片,使图片只是一个ImageView和它上面的TextView 。 (除非有更简单的方法可以做到这一点?)

  3. 最后,我需要将裁剪的图片保存到某种内存中,无论是SD卡,设备内存还是图库(不是更好)。

非常感谢任何帮助,非常感谢

   

这是我的xml代码

  

我的布局

     

我的Activity类

 public class MainActivity extends Activity { RelativeLayout rl; final int PIC_CROP = 1; ImageView iv; File file; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); rl = (RelativeLayout) findViewById(R.id.rl); Button b= (Button) findViewById(R.id.button1); iv= (ImageView) findViewById(R.id.imageView1); b.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { save(); } }); } public void save() { AlertDialog.Builder editalert = new AlertDialog.Builder(MainActivity.this); editalert.setTitle("Please Enter the name with which you want to Save"); final EditText input = new EditText(MainActivity.this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT); input.setLayoutParams(lp); editalert.setView(input); editalert.setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { rl.setDrawingCacheEnabled(true); String name= input.getText().toString(); Bitmap bitmap =rl.getDrawingCache(); String root = Environment.getExternalStorageDirectory().toString(); File myDir = new File(root + "/MyDraw"); myDir.mkdirs(); file = new File (myDir, name+".png"); if (file.exists ()) file.delete (); try { if(!file.exists()) { file.createNewFile(); } FileOutputStream ostream = new FileOutputStream(file); bitmap.compress(CompressFormat.PNG, 10, ostream); ostream.close(); Uri uri = Uri.fromFile(new File(file.getAbsolutePath())); performCrop(uri); } catch (Exception e) { e.printStackTrace(); } } }); editalert.show(); } private void performCrop(Uri picUri) { try { Intent cropIntent = new Intent("com.android.camera.action.CROP"); // indicate image type and Uri cropIntent.setDataAndType(picUri, "image/*"); // set crop properties cropIntent.putExtra("crop", "true"); // indicate aspect of desired crop cropIntent.putExtra("aspectX", 1); cropIntent.putExtra("aspectY", 1); // indicate output X and Y cropIntent.putExtra("outputX", 128); cropIntent.putExtra("outputY", 128); // retrieve data on return cropIntent.putExtra("return-data", true); // start the activity - we handle returning in onActivityResult startActivityForResult(cropIntent, PIC_CROP); } // respond to users whose devices do not support the crop action catch (ActivityNotFoundException anfe) { // display an error message String errorMessage = "Whoops - your device doesn't support the crop action!"; Toast toast = Toast.makeText(MainActivity.this, errorMessage, Toast.LENGTH_SHORT); toast.show(); } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == PIC_CROP) { if (data != null) { // get the returned data Bundle extras = data.getExtras(); // get the cropped bitmap Bitmap selectedBitmap = extras.getParcelable("data"); FileOutputStream ostream; try { ostream = new FileOutputStream(file); selectedBitmap.compress(CompressFormat.PNG, 10, ostream); ostream.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } iv.setImageBitmap(selectedBitmap); } } } 

快照

我的布局快照

在此处输入图像描述

在按钮上单击,提示用户输入要保存的名称

在此处输入图像描述

图片被裁剪并保存在图库中打开

在此处输入图像描述

注意:我已将裁剪后的图像保存在SD卡中。 快照尺寸各不相同。 这在我的设备上对我有用。 我发布了相同的内容。 如果有人有更好的想法请编辑上面的post。

manifest资源配置文件

             

嘿你可以使用下面的代码捕获android中的屏幕截图并保存在你的SD卡中,然后你可以在以后使用它

 // 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 = 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(); } 

然后,当您需要访问时使用以下内容:

 Uri uri = Uri.fromFile(new File(mPath));