2012-09-29 64 views
1

在我的應用程序我有一個選項,拍攝照片,將被保存在文件夾:爲什麼新創建的文件夾中的圖像沒有顯示出來?

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/my-images"; 

圖像會保存很好,我也有一個選項,選擇圖像使用這種代碼:

Intent intent = new Intent(); 
intent.setType("image/*"); 
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE); 

這裏最初是用來顯示2個畫廊

1.相機

2.我的圖像

選擇任何他們等的,都能正常運作。

但沒理由我刪除了my-images文件夾並再次運行應用程序。再次創建相同的文件夾並按預期保存圖像。

的問題是:

我的圖像畫廊沒有顯示出來,現在,當我點擊選擇圖像按鈕。

除了刪除my-images文件夾並重新啓動應用程序,我沒有改變任何代碼,因爲它曾經很好。

爲什麼這樣呢?

謝謝

編輯:

我的活動代碼,允許用戶拍照並存儲在SD卡上,並瀏覽圖片庫:

public class PictureFromAppActivity extends SherlockActivity { 
    private File dir; 
    private Bitmap photo; 
    private String encodedString; 
    private InputStream is; 
    private ImageView imagePreview; 
    private String selectedImagePath; 
    private static final int CAMERA_REQUEST = 1888; 
    private static final int SELECT_PICTURE = 1; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     imagePreview = (ImageView) findViewById(R.id.image_preview); 
     Button takePicture = (Button) findViewById(R.id.take_a_picture); 
     Button selectImage = (Button) findViewById(R.id.select_picture); 

     takePicture.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View arg0) { 

       if (android.os.Environment.getExternalStorageState().equals(
         android.os.Environment.MEDIA_MOUNTED)) { 

         Intent cameraIntent = new Intent(
           android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
         startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        } 
      } 
     }); 
     selectImage.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       Intent intent = new Intent(); 
       intent.setType("image/*"); 
       intent.setAction(Intent.ACTION_GET_CONTENT); 
       startActivityForResult(
         Intent.createChooser(intent, "Select Picture"), 
         SELECT_PICTURE); 
      } 
     }); 

    } 

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

     super.onActivityResult(requestCode, resultCode, data); 

     if (resultCode == RESULT_OK) { 
      if (requestCode == CAMERA_REQUEST) { 
       if (data != null) { 

        photo = (Bitmap) data.getExtras().get("data"); 
        Bitmap bitmapOrg = photo; 
        ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
        bitmapOrg.compress(Bitmap.CompressFormat.PNG, 100, bao); 
        byte[] ba = bao.toByteArray(); 

        try { 
         encodedString = Base64.encodeBytes(ba, 0); 
        } catch (IOException e1) { 
        } 

        if (android.os.Environment.getExternalStorageState() 
          .equals(android.os.Environment.MEDIA_MOUNTED)) { 

         File imagesFolder = new File(
           Environment 
             .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), 
           "/my-images"); 
         if (!imagesFolder.exists()) { 

          imagesFolder.mkdirs(); 
         } else { 

         } 
         String fileName = "image_" + count + ".jpg"; 
         f = new File(imagesFolder, fileName); 
         while (f.exists()) { 
          count++; 
          fileName = "image_" + String.valueOf(count) 
            + ".jpg"; 
          f = new File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) 
              + "/my-images", fileName); 
         } 
        } else { 

        } 
        f.createNewFile(); 

        FileOutputStream fo = new FileOutputStream(f); 
        fo.write(ba); 
        o.flush(); 
        fo.close(); 
        try { 
         File imageFile = new File(
           Environment 
             .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) 
             + "/my-images" + "/image_" + count + ".jpg"); 
         Bitmap bitmap = BitmapFactory.decodeFile(imageFile 
           .getAbsolutePath()); 
         imagePreview.setImageBitmap(bitmap); 
         Log.d("Image Width", "+" + imagePreview.getWidth()); 
         Log.d("Image Height", "+" + imagePreview.getHeight()); 
        } catch (Exception e) { 

        } 
       } else { 

        Intent intent = new Intent(PictureFromAppActivity.this, 
          PictureFromAppActivity.class); 
        startActivity(intent); 
       } 
      } else if (requestCode == SELECT_PICTURE) { 

       if (data != null) { 
        Uri selectedImageUri = data.getData(); 
        selectedImagePath = getPath(selectedImageUri); 
        try { 
         File imageFile = new File(selectedImagePath); 
         Bitmap bitmap = BitmapFactory.decodeFile(imageFile 
           .getAbsolutePath()); 
         imagePreview.setImageBitmap(bitmap); 
        } catch (Exception e) { 

        } 

       } else { 

       } 
      } 
     } 
    } 

    private String getPath(Uri selectedImageUri) { 

     String[] projection = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null); 
      int column_index = cursor 
        .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
      cursor.moveToFirst(); 
      return cursor.getString(column_index); 
    } 
} 

回答

4

五月那是因爲畫廊與您的應用程序相關聯,並且與您的應用程序保持一致。 因此,當刪除已從系統中刪除的圖像/文件夾時發生了什麼情況,但未更新與媒體庫相關的媒體庫。 請使用下面的代碼,讓我知道,如果這能解決你的問題

sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
       Uri.parse("file://" 
         + Environment.getExternalStorageDirectory()))); 

請使用此代碼從畫廊

intent = new Intent(
        Intent.ACTION_PICK, 
        android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); 
      startActivityForResult(intent, PICK_PHOTO); 

而在你的活動結果打開圖像

@Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
     String path = ""; 
switch (requestCode) { 
     case PICK_PHOTO: 
      if (resultCode == RESULT_OK) { 
       Uri uri = data.getData(); 
       path = pathFromUri(uri); // Now you can get the path of your selected image from here 
      } 
     break; 
     } 
    } 

從URI代碼獲取路徑

/** 
    * Method to get path from uri 
    * 
    * @param Uri 
    *   uri 
    * @return String path 
    * */ 
    private String pathFromUri(Uri uri) { 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
     Cursor cursor = getContentResolver().query(uri, filePathColumn, null, 
       null, null); 
     cursor.moveToFirst(); 
     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String filePath = cursor.getString(columnIndex); 
     cursor.close(); 
     return filePath; 
    } 
+0

在哪裏我應該使用此代碼? –

+0

您必須在打開庫以查看由您創建的圖像或文件夾時使用此代碼。這段代碼只是發送廣播刷新您的SD卡和圖書館查看您的文件夾/圖像 –

+0

所以之前打電話打開畫廊的意圖? –

相關問題