我想将Web中的图像保存在内部文件夹(数据/数据/ package_name / ...)中,但是在调用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$1.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
最佳答案
试试这个代码
位图相对位图;
公共无效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 Of保存此方法将被调用
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;
}
关于android - 不工作bitmap.compress,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29408448/