2013-01-18 32 views
2

嗨我想打開一個圖像只使用內置在Android中的圖庫。現在我使用下面的代碼,如果我點擊按鈕,它會顯示包含安裝的第三方工具的菜單以打開圖像。我只需要內置圖庫,是否有任何選項可以隱藏其他第三方工具,我可以直接使用圖庫打開而不顯示菜單。如何僅使用Built in gallery android打開圖像文件?

package com.example.gallery; 

import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class BrowsePicture extends Activity { 

private static final int SELECT_PICTURE = 1; 
Button bt; 
private String selectedImagePath; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_browse_picture); 

    bt = (Button) findViewById(R.id.button1); 
      bt.setOnClickListener(new OnClickListener() { 

       public void onClick(View arg0) { 

        // in onCreate or any event where your want the user to 
        // select a file 
        Intent intent = new Intent(); 
        intent.setType("image/*"); 
        intent.setAction(Intent.ACTION_GET_CONTENT); 
        startActivityForResult(
          Intent.createChooser(intent, "Select Picture"), 
          SELECT_PICTURE); 
       } 
      }); 
} 

public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if (resultCode == RESULT_OK) { 
     if (requestCode == SELECT_PICTURE) { 
      Uri selectedImageUri = data.getData(); 
      selectedImagePath = getPath(selectedImageUri); 
     } 
    } 
} 

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

} 

回答

1

這是因爲在按鈕的點擊你逝去的意圖在其中要設置其intent.setType("image/*");類型和intent.setAction(Intent.ACTION_GET_CONTENT);所以它會顯示安裝在設備的應用程序列表。

如果您只想直接打開圖庫,那麼您應該只傳遞圖庫的意圖。

+0

謝謝。對於gaalery我應該通過什麼意圖?你可以請張貼圖庫的意圖? – Krishna

+0

看看這個.. http://stackoverflow.com/questions/3864860/how-to-open-gallery-via-intent-without-result – Rahil2952

+0

@Krishna意圖我=新的意圖(Intent.ACTION_PICK, android.provider .MediaStore.Images.Media.INTERNAL_CONTENT_URI);或Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse( 「content:// media/internal/images/media」));將對你有用。讓我知道這是否有助於你。 – Rahil2952

6

嘗試使用

Intent intent = new Intent(Intent.ACTION_PICK); 
intent.setType("image/*"); 
startActivityForResult(intent, SELECT_PICTURE); 
相關問題