如何从画廊中仅选择相机捕捉图像?

时间:2016-11-25 10:03:26

标签: android

我想只从画廊中选择那些从相机中点击的图像,而不是来自其他来源的图像。

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

此查询为我提供了所有图像。

我希望相机图像的路径不受画廊和相机意图的影响。是否有一些通用方法可以返回捕获图像路径的路径

1 个答案:

答案 0 :(得分: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);

}

 }