无法将图片添加到新文件夹

时间:2012-03-09 01:59:32

标签: android

将图片添加到新文件夹时遇到问题。出于某种原因,当我的手机连接到计算机并用作模拟器时,图片确实会保存到正确的文件夹中。但是,一旦我将手机与电脑断开连接,它就不会将图片保存到文件夹中。我已经包含以下权限:

<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> 

我的代码如下:

public class Camera extends Activity implements View.OnClickListener{
Intent i;
ImageView iv;
ImageButton ib;
Button b;
final static int cameraData = 0;
Bitmap bmp;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.camera);
    initialize();
    InputStream is = getResources().openRawResource(R.drawable.ic_launcher);
    bmp= BitmapFactory.decodeStream(is);
}

private void initialize() {
    // TODO Auto-generated method stub
    iv =(ImageView) findViewById(R.id.ivReturnPic);
    ib = (ImageButton) findViewById(R.id.ibTakePic);
    b = (Button)findViewById(R.id.bSetWall);
    b.setOnClickListener(this);
    ib.setOnClickListener(this);
}

public void onClick(View v) {
    // TODO Auto-generated method stub
    switch(v.getId()){
    case R.id.bSetWall:
        try {
            getApplicationContext().setWallpaper(bmp);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        break;
    case R.id.ibTakePic:
        int imageNum = 0;
        Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        File imagesFolder = new File(Environment.getExternalStorageDirectory(), "BeautifulPlaces");
        imagesFolder.mkdirs(); // <----
        String fileName = "image_" + String.valueOf(imageNum) + ".jpg";
        File output = new File(imagesFolder, fileName);
        while (output.exists()){
            imageNum++;
            fileName = "image_" + String.valueOf(imageNum) + ".jpg";
            output = new File(imagesFolder, fileName);
        }
        Uri uriSavedImage = Uri.fromFile(output);
        imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
        startActivityForResult(imageIntent, 0);

    }
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        Bundle extras = data.getExtras();
        bmp = (Bitmap) extras.get("data");
        iv.setImageBitmap(bmp);
    }
}
}

1 个答案:

答案 0 :(得分:0)

在向其写入数据之前,您必须检查外部存储。因为它可能不可用。

您可以参考此文档:http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory()

String extStorageState = Environment.getExternalStorageState();
if (extStorageState.equalsIgnoreCase(Environment.MEDIA_MOUNTED))  {
    //  For ensure there is no problem, I usually do one more checking here
    File externalDir = Environment.getExternalStorageDirectory();
    if (externalDir.canWrite())  {
        //  Do you job here
        //  ...            

    }
}