各位好。帮忙看一下,我这个代码应该如何修改才能让图片保存成功!!!谢谢。package gdaib.shofware.pard;import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;public class Screenshoot1Activity extends Activity {
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ScreenShot be=new ScreenShot();
be.shoot(Screenshoot1Activity.this);
}
});
}
}////
、/////////package gdaib.shofware.pard;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;public class ScreenShot {
//static SimpleDateFormat data=new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss",Locale.US);
//static String fname="/sdcard/"+data.format(new Date())+".png";
static String fname="/sdcard/"+"a.png";
private static final String TAG="shotscreen";
@SuppressWarnings("unused")
private static Bitmap takeScreenShot(Activity activity){
View view =activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
System.out.println(statusBarHeight);
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
Bitmap bitmap2 = Bitmap.createBitmap(bitmap,0,statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return bitmap2;
}
@SuppressWarnings("unused")
private static void savePic(Bitmap bitmap2,String filename){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filename);
if (fileOutputStream != null) {
bitmap2.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}
}
catch (FileNotFoundException e) {
Log.d(TAG, "Exception:FileNotFoundException");
e.printStackTrace();
}
catch (IOException e) {
Log.d(TAG, "IOException:IOException");
e.printStackTrace();
}
}
public static void shoot(Activity a){
if (android.os.Environment.MEDIA_MOUNTED != "mounted") {
ScreenShot.savePic(ScreenShot.takeScreenShot(a), fname);
}
else{
ScreenShot.savePic(ScreenShot.takeScreenShot(a), "/data/data/"+a.getPackageName()+"/抽样.png");
}
}
}
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;public class Screenshoot1Activity extends Activity {
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
ScreenShot be=new ScreenShot();
be.shoot(Screenshoot1Activity.this);
}
});
}
}////
、/////////package gdaib.shofware.pard;import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import android.view.View;public class ScreenShot {
//static SimpleDateFormat data=new SimpleDateFormat("yyyy-MM-dd_HH.mm.ss",Locale.US);
//static String fname="/sdcard/"+data.format(new Date())+".png";
static String fname="/sdcard/"+"a.png";
private static final String TAG="shotscreen";
@SuppressWarnings("unused")
private static Bitmap takeScreenShot(Activity activity){
View view =activity.getWindow().getDecorView();
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
Rect rect = new Rect();
activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
int statusBarHeight = rect.top;
System.out.println(statusBarHeight);
int width = activity.getWindowManager().getDefaultDisplay().getWidth();
int height = activity.getWindowManager().getDefaultDisplay().getHeight();
Bitmap bitmap2 = Bitmap.createBitmap(bitmap,0,statusBarHeight, width, height - statusBarHeight);
view.destroyDrawingCache();
return bitmap2;
}
@SuppressWarnings("unused")
private static void savePic(Bitmap bitmap2,String filename){
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(filename);
if (fileOutputStream != null) {
bitmap2.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
}
}
catch (FileNotFoundException e) {
Log.d(TAG, "Exception:FileNotFoundException");
e.printStackTrace();
}
catch (IOException e) {
Log.d(TAG, "IOException:IOException");
e.printStackTrace();
}
}
public static void shoot(Activity a){
if (android.os.Environment.MEDIA_MOUNTED != "mounted") {
ScreenShot.savePic(ScreenShot.takeScreenShot(a), fname);
}
else{
ScreenShot.savePic(ScreenShot.takeScreenShot(a), "/data/data/"+a.getPackageName()+"/抽样.png");
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货