当前位置: 代码迷 >> Android >> android部分真的就没人解决这个有关问题嘛
  详细解决方案

android部分真的就没人解决这个有关问题嘛

热度:42   发布时间:2016-05-01 15:10:45.0
android部分真的就没人解决这个问题嘛!
Java code
package com.examples.jieping;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileOutputStream;import android.os.Bundle;import android.os.Environment;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.drawable.BitmapDrawable;import android.view.Display;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.Toast;public class MainActivity extends Activity {        private Button shotButton = null;        private ImageView imageView = null;        @Override        public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.activity_main);            shotButton=(Button)findViewById(R.id.shotButton);            imageView=(ImageView)findViewById(R.id.imageView);            shotButton.setOnClickListener(new OnClickListener() {                public void onClick(View v) {                    //Bitmap-->Drawable                    BitmapDrawable bd=new BitmapDrawable(shot());                    imageView.setBackgroundDrawable(bd);                    String filename = Environment.getExternalStorageState().toString()                            +"/"                            +"ar"+"/"                            +System.currentTimeMillis()+".jpg";                    File file = new File(filename);                    if(!file.getParentFile().exists()){                        file.getParentFile().mkdirs();                    }                    try {                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));                        shot().compress(Bitmap.CompressFormat.JPEG, 80, bos);                        bos.flush();                        bos.close();                        Toast.makeText(MainActivity.this, "拍照成功", Toast.LENGTH_SHORT).show();                    } catch (Exception e) {                        // TODO Auto-generated catch block                        Toast.makeText(MainActivity.this, "拍照失败", Toast.LENGTH_SHORT).show();                    }//                    imageView.setImageBitmap(shot());                }            });        }        /**         * 截屏方法         * @return         */        private Bitmap shot() {            View view = getWindow().getDecorView();            Display display = this.getWindowManager().getDefaultDisplay();            view.layout(0, 0, display.getWidth(), display.getHeight());            view.setDrawingCacheEnabled(true);//允许当前窗口保存缓存信息,这样getDrawingCache()方法才会返回一个Bitmap            Bitmap bmp = Bitmap.createBitmap(view.getDrawingCache());            return bmp;        }        @Override    public boolean onCreateOptionsMenu(Menu menu) {        getMenuInflater().inflate(R.menu.activity_main, menu);        return true;    }}


Java code
<manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.examples.jieping"    android:versionCode="1"    android:versionName="1.0" >    <uses-sdk        android:minSdkVersion="8"        android:targetSdkVersion="15" />    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <application        android:icon="@drawable/ic_launcher"        android:label="@string/app_name"        android:theme="@style/AppTheme" >        <activity            android:name=".MainActivity"            android:label="@string/title_activity_main" >            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest>
  相关解决方案