2015-11-08 36 views
0

今天早上我被困在這裏:圖像沒有被創建在文件夾中。
你能幫我,告訴我爲什麼它不起作用嗎?圖像沒有被保存在文件夾中

這是正確的獲取圖像?

Bundle extras = data.getExtras(); 
     Bitmap imageToSave = extras.getParcelable("data"); 

這是我的全部代碼

@Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

     super.onActivityResult(requestCode, resultCode, data); 

     if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) { 



      final File path = 
        Environment.getExternalStoragePublicDirectory 
          (
            // Environment.DIRECTORY_PICTURES + "/ss/" 
            //Environment.DIRECTORY_DCIM 
            Environment.DIRECTORY_DCIM + "/MyFolderName/" 
          ); 

      // Make sure the Pictures directory exists. 
      if(!path.exists()) 
      { 
       path.mkdirs(); 
      } 
      // Bitmap imageToSave = (Bitmap) data.getExtras().get("data"); 
      // Bitmap imageToSave = (Bitmap) data.getData(); 
      Bundle extras = data.getExtras(); 
      Bitmap imageToSave = extras.getParcelable("data"); 

      final File file = new File(path, "file" + ".jpg"); 
      try { 
       FileOutputStream fos = new FileOutputStream(path); 
       final BufferedOutputStream bos = new BufferedOutputStream(fos, 8192); 

       FileOutputStream out = new FileOutputStream(path); 
       //fos = new FileOutputStream(path); 
       imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, fos); 
       // imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out); 
       out.flush(); 
       out.close(); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      Uri selectedImage = data.getData(); 

      Intent i = new Intent(this, 
        AddImage.class); 
      i.putExtra("imagePath", selectedImage.toString()); 
      startActivity(i); 


     } 
    }} 

回答

2

這應有助於:

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{ 
    super.onActivityResult(requestCode, resultCode, data); 

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) 
    { 
     Uri selectedImage = data.getData(); 
     String[] filePathColumn = {MediaStore.Images.Media.DATA}; 

     Cursor cur = 
      getContentResolver().query 
      (
       selectedImage, filePathColumn, null, null, null 
      ); 
     cur.moveToFirst(); 

     String picturePath = cur.getString(cur.getColumnIndex(filePathColumn[0])); 
     cur.close(); 

     // String picturePath contains the path of the selected Image 
     // Now you can copy it, send it to a server, load it into an ImageView... 
    } 
} 
+0

我複製你的代碼,因爲它是,並且圖像wasent保存,我缺少什麼,你是如何指定的文件夾名稱? – Moudiz

+0

關於它的更多信息:http://stackoverflow.com/a/8737101/2649012 –

+1

我對你的代碼進行了更多的搜索,並且我得到了這個例子,它也幫助了我http://www.geeks.gallery/saving-image/ ..謝謝你幫助我 – Moudiz

1

Data.getExtra() retrun一個Uri;你應該把它轉換爲filepath

相關問題