2016-11-25 111 views
-1

我想只選擇那些從相機點擊的圖片,而不是其他來源的圖片。如何僅從畫廊中選擇相機捕捉圖像?

Cursor imagecursor = getApplicationContext().getContentResolver().query(
       android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 
       new String[] {MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID}, null, 
       null, null); 

此查詢爲我提供了所有圖像。

我想irrespectice畫廊和相機intent.Is有返回拍攝的圖像路徑

+0

試試這個http://stackoverflow.com/questions/4484158/list-all-camera-images-in-android –

+0

我想要的路徑,畫廊和相機intent.Is的攝像機圖像irrespectice有一些通用的方法返回拍攝圖像路徑的路徑。 –

+0

你可以得到圖片的公共目錄的路徑,所以這可能有助於http://stackoverflow.com/questions/16993213/how-can-i-find-out-the-camera-images-folder-of-an-android -電話 –

回答

0

下面是一個例子希望它幫助的路徑一些通用的方法路徑攝像機圖像!

package com.mahesh.gallerytestapp; 


public class MainActivity extends Activity { 

Uri selectedImageUri; 
String selectedPath; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
Button b = (Button) findViewById(R.id.bGallery); 
Button bCam= (Button) findViewById(R.id.bCamera); 
ImageView preview = findViewById(R.id.preview); 
bCam.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(cameraIntent, 100); 
} 
}); 


b.setOnClickListener(new OnClickListener() { 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
openGallery(10); 
} 
}); 
} 



public void openGallery(int req_code){ 

    Intent intent = new Intent(); 

    intent.setType("image/*"); 

    intent.setAction(Intent.ACTION_GET_CONTENT); 

    startActivityForResult(Intent.createChooser(intent,"Select file to upload "), req_code); 

} 

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



    if (resultCode == RESULT_OK) { 
    if(data.getData() != null){ 
     selectedImageUri = data.getData(); 
    }else{ 
     Log.d("selectedPath1 : ","Came here its null !"); 
     Toast.makeText(getApplicationContext(), "failed to get Image!", 500).show(); 
    } 

    if (requestCode == 100 && resultCode == RESULT_OK) { 
      Bitmap photo = (Bitmap) data.getExtras().get("data"); 
      selectedPath = getPath(selectedImageUri); 
      preview.setImageURI(selectedImageUri); 
      Log.d("selectedPath1 : " ,selectedPath); 

     } 

     if (requestCode == 10) 

     { 

      selectedPath = getPath(selectedImageUri); 
      preview.setImageURI(selectedImageUri); 
      Log.d("selectedPath1 : " ,selectedPath); 

     } 

    } 

} 


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

} 

} 
相關問題