-3

我想保存我绘制的图像,但总是报告失败:

09-12 07:30:34.346: E / Panel (8003): IOEception
09-12 07:30:34.346: E / Panel (8003): java.io.IOException: Parent directory of file does not exist: / sdcard/anppp/2012Sep1273034.png
09-12 07:30:34.346: E / Panel (8003): at java.io.File.createNewFile (File.java: 1263)
09-12 07:30:34.346: E / Panel (8003): at aa.bb.cc.Panel.saveapp (Panel.java: 67)
09-12 07:30:34.346: E / Panel (8003): at aa.bb.cc.AndroidPaint.onOptionsItemSelected (AndroidPaint.java: 94)
09-12 07:30:34.346: E / Panel (8003): at android.app.Activity.onMenuItemSelected (Activity.java: 2170)
09-12 07:30:34.346: E / Panel (8003): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected (PhoneWindow.java: 730)
09-12 07:30:34.346: E / Panel (8003): at com.android.internal.view.menu.MenuItemImpl.invoke (MenuItemImpl.java: 139)
09-12 07:30:34.346: E / Panel (8003): at com.android.internal.view.menu.MenuBuilder.performItemAction (MenuBuilder.java: 855)
09-12 07:30:34.346: E / Panel (8003): at com.android.internal.view.menu.ExpandedMenuView.invokeItem (ExpandedMenuView.java: 89)
09-12 07:30:34.346: E / Panel (8003): at com.android.internal.view.menu.ExpandedMenuView.onItemClick (ExpandedMenuView.java: 93)
09-12 07:30:34.346: E / Panel (8003): at android.widget.AdapterView.performItemClick (AdapterView.java: 284)
09-12 07:30:34.346: E / Panel (8003): at android.widget.ListView.performItemClick (ListView.java: 3285)
09-12 07:30:34.346: E / Panel (8003): at android.widget.AbsListView $ PerformClick.run (AbsListView.java: 1640)
09-12 07:30:34.346: E / Panel (8003): at android.os.Handler.handleCallback (Handler.java: 587)
09-12 07:30:34.346: E / Panel (8003): at android.os.Handler.dispatchMessage (Handler.java: 92)
09-12 07:30:34.346: E / Panel (8003): at android.os.Looper.loop (Looper.java: 123)
09-12 07:30:34.346: E / Panel (8003): at android.app.ActivityThread.main (ActivityThread.java: 4363)
09-12 07:30:34.346: E / Panel (8003): at java.lang.reflect.Method.invokeNative (Native Method)
09-12 07:30:34.346: E / Panel (8003): at java.lang.reflect.Method.invoke (Method.java: 521)
09-12 07:30:34.346: E / Panel (8003): at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java: 860)
09-12 07:30:34.346: E / Panel (8003): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java: 618)
09-12 07:30:34.346: E / Panel (8003): at dalvik.system.NativeStart.main (Native Method)

我的代码:

private Bitmap mBitmap;
private Canvas mCanvas;
private Bitmap tmpBitmap;
private Canvas tmpCanvas;
private DrawHandler mDrawHandler;
private Canvas tCanvas;
private String mImagePath = Environment.getExternalStorageDirectory() + "/anppp";
private File file;

public void saveapp() {

    Calendar currentDate = Calendar.getInstance();
    SimpleDateFormat formatter= new SimpleDateFormat("yyyyMMMddHmmss");
    String dateNow = formatter.format(currentDate.getTime());
    file = new File(mImagePath + "/" + dateNow +".png");
    FileOutputStream fos;
    try {
        file.createNewFile();
        fos = new FileOutputStream(file);
        tmpBitmap.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
        } catch (FileNotFoundException e) {
        Log.e("Panel", "FileNotFoundException", e);
    }
    catch (IOException e) {
        Log.e("Panel", "IOEception", e);
    }
}

就是这样..我不知道可能出了什么问题;(

4

1 回答 1

1

我认为您的 SD 卡中没有名为 anppp 的文件夹。所以我建议你首先使用

File dirPath = new File(Environment.getExternalStorageDirectory().toString() + "/anppp");
dirPath.mkdirs();

现在执行您正在执行的任务。希望这可以帮助你

于 2012-09-15T15:44:14.900 回答