2012-03-09 59 views
1

我無法將圖片添加到新文件夾。出於某種原因,當我的手機連接到計算機並用作模擬器時,圖片確實會被保存到正確的文件夾中。但是,一旦我從電腦斷開手機,它不會將照片保存到文件夾中。我已經包括以下權限:無法將圖片添加到新文件夾

<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); 
    } 
} 
} 

回答

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 
     // ...    

    } 
} 
+0

我加了檢查,以確保外部存儲可以作爲再次建議當手機連接到電腦,但不是在其斷開的程序工作。 – user1258244 2012-03-09 07:50:57

+0

有沒有我失蹤的許可?因爲它在連接到計算機時工作時很奇怪,或者是因爲應用程序未被簽名,但它仍然運行,所以應該是它。 – user1258244 2012-03-09 10:20:42

+0

我認爲你應該在onActivityResult中做你的保存過程。由於圖像數據在攝像機活動完成之前不會傳回。 – Hanon 2012-03-09 10:24:34