不工作 bitmap.compress
Dont work bitmap.compress
我想在内部文件夹 (data/data/package_name/...) 中保存来自 Web 的图像,但是当我调用 Bitmap.compress()
方法时我捕获了异常。请帮我找出我的错误。
这是我的代码:
FileOutputStream out = null;
try {
File fileDir = new File(PATH_FOR_IMAGES);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
File file = new File(PATH_FOR_IMAGES + nameOfBitmap);
if (!file.exists()) {
file.createNewFile();
}
out = new FileOutputStream(file);
final FileOutputStream finalOut = out;
final FileOutputStream finalOut1 = out;
new Thread(new Runnable() {
@Override
public void run() {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, finalOut);
try {
if (finalOut1 != null) {
finalOut1.close();
}
} catch (Exception e) {
throw new RuntimeException();
}
}
}).start();
} catch (Exception e) {
try {
if (out != null) {
out.close();
}
} catch (Exception e1) {}
throw new RuntimeException();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {}
}
清单中的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
日志中出现此错误 printStackTrace()
:
D/skia﹕ ------- write threw an exception
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ java.io.IOException: write failed: EBADF (Bad file number)
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.IoBridge.write(IoBridge.java:462)
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at java.io.FileOutputStream.write(FileOutputStream.java:187)
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at android.graphics.Bitmap.nativeCompress(Native Method)
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at android.graphics.Bitmap.compress(Bitmap.java:904)
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at com.DriverNotes.AndroidMobileClient.extra.SponsorManager.run(SponsorManager.java:330)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ Caused by: libcore.io.ErrnoException: write failed: EBADF (Bad file number)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.Posix.writeBytes(Native Method)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.Posix.write(Posix.java:187)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.BlockGuardOs.write(BlockGuardOs.java:197)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.IoBridge.write(IoBridge.java:457)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ ... 5 more
试试这个代码
位图relativeBitmap;
public void saveFile()
{
View view=(RelativeLayout)findViewById(R.id.relativeLayout);
relativeBitmap= takeSnapshot(view);
OutputStream output=null;
//Find the SD Card path
File filePath = Environment.getExternalStorageDirectory();
// Create a new folder in SD Card
File dir = new File(filePath.getAbsolutePath()+ "/Save Image Frame/");
if(!dir.exists())
dir.mkdirs();
// Create a name for the saved image
File file = (File) new File(dir, "myimage.png");
// Show a toast message on successful save
Toast.makeText(MainActivity.this, "Image Saved to SD Card",
Toast.LENGTH_SHORT).show();
try
{
output=new FileOutputStream(file);
// Compress into png format image from 0% - 100%
relativeBitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
//onClick的save这个方法会被调用
private Bitmap takeSnapshot(View v)
{
if(bitmap!=null)
{
bitmap.recycle();
bitmap=null;
}
bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
/*c.drawBitmap(v.getDrawingCache(), 0, 0, null);
v.destroyDrawingCache();*/
v.draw(c);
return bitmap;
}
我想在内部文件夹 (data/data/package_name/...) 中保存来自 Web 的图像,但是当我调用 Bitmap.compress()
方法时我捕获了异常。请帮我找出我的错误。
这是我的代码:
FileOutputStream out = null;
try {
File fileDir = new File(PATH_FOR_IMAGES);
if (!fileDir.exists()) {
fileDir.mkdirs();
}
File file = new File(PATH_FOR_IMAGES + nameOfBitmap);
if (!file.exists()) {
file.createNewFile();
}
out = new FileOutputStream(file);
final FileOutputStream finalOut = out;
final FileOutputStream finalOut1 = out;
new Thread(new Runnable() {
@Override
public void run() {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, finalOut);
try {
if (finalOut1 != null) {
finalOut1.close();
}
} catch (Exception e) {
throw new RuntimeException();
}
}
}).start();
} catch (Exception e) {
try {
if (out != null) {
out.close();
}
} catch (Exception e1) {}
throw new RuntimeException();
} finally {
try {
if (out != null) {
out.close();
}
} catch (Exception e) {}
}
清单中的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
日志中出现此错误 printStackTrace()
:
D/skia﹕ ------- write threw an exception
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ java.io.IOException: write failed: EBADF (Bad file number)
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.IoBridge.write(IoBridge.java:462)
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at java.io.FileOutputStream.write(FileOutputStream.java:187)
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at android.graphics.Bitmap.nativeCompress(Native Method)
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at android.graphics.Bitmap.compress(Bitmap.java:904)
04-02 11:25:44.702 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at com.DriverNotes.AndroidMobileClient.extra.SponsorManager.run(SponsorManager.java:330)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at java.lang.Thread.run(Thread.java:856)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ Caused by: libcore.io.ErrnoException: write failed: EBADF (Bad file number)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.Posix.writeBytes(Native Method)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.Posix.write(Posix.java:187)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.BlockGuardOs.write(BlockGuardOs.java:197)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ at libcore.io.IoBridge.write(IoBridge.java:457)
04-02 11:25:44.712 5627-6056/com.DriverNotes.AndroidMobileClient W/System.err﹕ ... 5 more
试试这个代码
位图relativeBitmap;
public void saveFile() {
View view=(RelativeLayout)findViewById(R.id.relativeLayout);
relativeBitmap= takeSnapshot(view);
OutputStream output=null;
//Find the SD Card path
File filePath = Environment.getExternalStorageDirectory();
// Create a new folder in SD Card
File dir = new File(filePath.getAbsolutePath()+ "/Save Image Frame/");
if(!dir.exists())
dir.mkdirs();
// Create a name for the saved image
File file = (File) new File(dir, "myimage.png");
// Show a toast message on successful save
Toast.makeText(MainActivity.this, "Image Saved to SD Card",
Toast.LENGTH_SHORT).show();
try
{
output=new FileOutputStream(file);
// Compress into png format image from 0% - 100%
relativeBitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
output.flush();
output.close();
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
e.printStackTrace();
}
}
//onClick的save这个方法会被调用
private Bitmap takeSnapshot(View v)
{
if(bitmap!=null)
{
bitmap.recycle();
bitmap=null;
}
bitmap = Bitmap.createBitmap(v.getWidth(), v.getHeight(),Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
/*c.drawBitmap(v.getDrawingCache(), 0, 0, null);
v.destroyDrawingCache();*/
v.draw(c);
return bitmap;
}