0

我想将图像保存到 sd 卡中。但是在 sd 卡中仅创建了命名图像文件的文件夹,但未在 sd 卡中创建实际文件。尝试保存该图像时出现空指针异常到 SD 卡。这是代码:

public class MainActivity extends Activity {
    ImageView bmImage; 
    LinearLayout view1;
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);

          view1 = (LinearLayout)findViewById(R.id.certlinear);
          bmImage = (ImageView)findViewById(R.id.certimage);

        Button button =(Button)findViewById(R.id.btn);
        button.setOnClickListener(hello);

    }
        Button.OnClickListener hello= new Button.OnClickListener()
        {  
            @Override  
            public void onClick(View v)
            {   
                  View view = view1.getRootView();
                  //u can use any view of your View instead of TextView

                 if(view!=null)
                 {
                 System.out.println("view is not null.....");
                 view.setDrawingCacheEnabled(true);
                 view.buildDrawingCache();
                 Bitmap bm = view.getDrawingCache();

                 try
                 {
                 if(bm!=null)
                 {
                 System.out.println("bm is not null.....");
                 OutputStream fos = null;
                 File folder = new File(Environment.getExternalStorageDirectory().toString() + "/sample.JPEG");
                 boolean success = false;
                 if (!folder.exists()) {
                     success = folder.mkdirs();
                 }
                 if (!success) {
                     // Do something on success
                 } else {
                     // Do something else on failure 
                 }
                 Toast.makeText(MainActivity.this,"path of the folder is "+folder.getAbsolutePath(), Toast.LENGTH_SHORT).show();

                 fos = new FileOutputStream(folder);

                 BufferedOutputStream bos = new BufferedOutputStream(fos);
                 bm.compress(Bitmap.CompressFormat.JPEG, 50, bos);

                 bos.flush();
                 bos.close();
                 view.setDrawingCacheEnabled(false);
                 }
                 }
                 catch(Exception e)
                 {
                 System.out.println("Error="+e);
                 e.printStackTrace();
                 }
                 }

            }
        };
}

这是异常日志:

09-13 18:44:18.957: W/System.err(3612): java.io.FileNotFoundException: /sdcard/sample.JPEG
09-13 18:44:18.967: W/System.err(3612):     at org.apache.harmony.luni.platform.OSFileSystem.open(OSFileSystem.java:244)
09-13 18:44:18.967: W/System.err(3612):     at java.io.FileOutputStream.<init>(FileOutputStream.java:97)
09-13 18:44:18.967: W/System.err(3612):     at java.io.FileOutputStream.<init>(FileOutputStream.java:69)
09-13 18:44:18.976: W/System.err(3612):     at com.example.mytest.MainActivity$1.onClick(MainActivity.java:96)
09-13 18:44:18.976: W/System.err(3612):     at android.view.View.performClick(View.java:2364)
09-13 18:44:18.976: W/System.err(3612):     at android.view.View.onTouchEvent(View.java:4179)
09-13 18:44:18.976: W/System.err(3612):     at android.widget.TextView.onTouchEvent(TextView.java:6541)
09-13 18:44:18.976: W/System.err(3612):     at android.view.View.dispatchTouchEvent(View.java:3709)
09-13 18:44:18.976: W/System.err(3612):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-13 18:44:18.987: W/System.err(3612):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-13 18:44:18.987: W/System.err(3612):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-13 18:44:18.987: W/System.err(3612):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
09-13 18:44:18.997: W/System.err(3612):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
09-13 18:44:18.997: W/System.err(3612):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
09-13 18:44:18.997: W/System.err(3612):     at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
09-13 18:44:18.997: W/System.err(3612):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
09-13 18:44:18.997: W/System.err(3612):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
09-13 18:44:18.997: W/System.err(3612):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-13 18:44:18.997: W/System.err(3612):     at android.os.Looper.loop(Looper.java:123)
09-13 18:44:18.997: W/System.err(3612):     at android.app.ActivityThread.main(ActivityThread.java:4363)
09-13 18:44:19.011: W/System.err(3612):     at java.lang.reflect.Method.invokeNative(Native Method)
09-13 18:44:19.011: W/System.err(3612):     at java.lang.reflect.Method.invoke(Method.java:521)
09-13 18:44:19.011: W/System.err(3612):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-13 18:44:19.011: W/System.err(3612):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-13 18:44:19.011: W/System.err(3612):     at dalvik.system.NativeStart.main(Native Method)

我在这一行得到空指针异常:

   fos = new FileOutputStream(folder);

提前致谢。

4

4 回答 4

3

您是否添加了此权限

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2012-09-13T13:29:03.950 回答
3

你打电话

success = folder.mkdirs();

这将创建一个名为 /sdcard 的文件夹(存在)和一个文件夹名称 /sdcard/sample.JPEG 您要写入图像的路径是一个目录。您不能在目录上打开 OutputStream。

于 2012-09-13T13:30:05.550 回答
1

只需在您的代码中进行简单的更改,请尝试以下操作,

File folder = new File(Environment.getExternalStorageDirectory().toString());
boolean success = false;
if (!folder.exists()) {
    success = folder.mkdirs();
}

File file = new File(Environment.getExternalStorageDirectory().toString() + "/sample.JPEG");
if (!file.exists()) {
    success = file.createNewFile();
}
于 2012-09-13T13:32:34.713 回答
0

这在我的项目中效果很好:

private void saveChicaDress(Bitmap dress){

        String path = Environment.getExternalStorageDirectory().toString() + 
                        getResources().getString(R.string.path_to_store);

        OutputStream fOut = null;
        File file = new File(path, "chica.png");

        int i = 1;  
        while(file.exists()){
            file = new File(path, "chica("+String.valueOf(i)+").png");
            ++i;
        }

        file.getParentFile().mkdirs();

        try {
            fOut = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        dress.compress(Bitmap.CompressFormat.PNG, 100, fOut);

        try {
            fOut.flush();
            fOut.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
}

在 AndroidManifest 中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2012-09-13T13:37:43.323 回答