Get all the images from the Gallery in Android - java

I want to make my own Gallery in Android but I can´t find the way to bring all the photos without selecting them. I have tried this:
Intent galleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, RESULT_LOAD_IMG);
But opens an intent in which I have to select one or more photos. Is there a way I can bring them all without selecting them?

This is a sample code for it
public class GetImageActivity extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView img;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img = (ImageView)findViewById(R.id.ImageView01);
((Button) findViewById(R.id.Button01))
.setOnClickListener(new OnClickListener() {
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);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(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);
}
}
For your question hell of solutions is available in SOF. So before posting a new question google it first.look into these examples if you face any problem..
android gallery into grid style menu
GridView loading photos from SD Card
How to implement Image Gallery in Gridview in android?
Android-Fetch images from sdcard and display in gridview

Related

how to get real path image from sdcard after cropping?

i have problem in my project. i want to pass cropped image to other activity. i've done my crop method but i cant get the real path of it. i've searched before how to get the path but all ways that i found didnt work. its always give me null path. so if you guys can help me, please answer my question..
here is my code
public class MainActivity extends AppCompatActivity {
Button btn;
ImageView imgView;
private Uri mImageCaptureUri;
String realPath;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.button);
imgView = (ImageView) findViewById(R.id.imageView);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
i.putExtra("crop","true");
i.putExtra("aspectX", 1);
i.putExtra("aspectY", 1);
i.putExtra("outputX", 200);
i.putExtra("outputY", 200);
i.putExtra("return-data", true);
startActivityForResult(i,2);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 2 && resultCode == RESULT_OK && data != null){
Uri uri = data.getData();
String type = data.getType();
Log.i("TAG", "Pick completed: " + uri + " " + type);
if (uri != null)
{
String path = uri.toString();
if (path.toLowerCase().startsWith("file://"))
{
// Selected file/directory path is below
path = (new File(URI.create(path))).getAbsolutePath();
Log.e("ini", path);
}
}
Bundle extras = data.getExtras();
Bitmap img = extras.getParcelable("data");
imgView.setImageBitmap(img);
}
}
if i got the real path, i can pass the path using intent and apply it to the next imageview. all i need is the path. thanks
Source URI present in data extraBundle.
Bundle extras = data.getExtras();
Bitmap img = extras.getParcelable("data");
imgView.setImageBitmap(img);
Uri selectedImageUri = Uri.parse(extras.get("src_uri").toString());
String realPath = getRealPathFromURI(selectedImageUri);
public String getRealPathFromURI(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
#SuppressWarnings("deprecation")
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}

Take photo and save path

I know that probably this question is already here but i didnt find anything that could help me. I wanted to take a photo and save its path. I'm already taking the photo but i cant show the path in the Toast or save it in the database.
private static final int TAKE_PICTURE = 1;
private Uri outputFileUri;
SQLiteDatabase mydb;
static Uri capturedImageUri = null;
ImageView ecran;
Button b2,vertudo;
private String path;
ArrayList data;
ListView lista;
public void onClick(View v) {
try{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),"test.jpg");
outputFileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, TAKE_PICTURE);
}catch(Exception e){
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
try{
if (resultCode == RESULT_OK) {
if (requestCode == TAKE_PICTURE) {
outputFileUri = data.getData();
path = getPath(outputFileUri);
mydb.execSQL("INSERT INTO caminho(nome) VALUES('"+path+"');");
Toast.makeText(getApplicationContext(), "Sucesso " + path,Toast.LENGTH_LONG).show();
}
}
}catch(Exception e){
Toast.makeText(getApplicationContext(),"Nao",Toast.LENGTH_LONG).show();
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor =getContentResolver().query(uri, projection, null,null,null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
What you are trying to do in
path = getPath(outputFileUri);
I think data.getData() in onActivityResult wil directly returns the path of the captured image.

Android API Level 8 Pick up the last taken Photo form specific folder

I would like to take a Photo that was Caputred with my App and send it via SFTP. I'm putting the Photo to an Specific folder:
timeStamp = new SimpleDateFormat("yyyyMMDD_HHmmss").format(new Date());
root = new File(Environment.getExternalStorageDirectory()+ File.separator + "OpenClinica" + File.separator);
root.mkdirs();
sdDir = new File(root, "OC_" + timeStamp + ".jpg");
Now I need to take this picture by a click of a button and send it via SFTP.
I have the classes/methods for SFTP, but I can not get the file selector.
Thank you for Helping
Try this code:
#Override
public void onClick(View v) {
if (v.getId() == findViewById(R.id.ID).getId()){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Seleccionar vídeo"), PICK_IMAGE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && data != null && data.getData() != null) {
Uri _uri = data.getData();
//User had pick an video.
Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
//Link to the video
final String imageFilePath = cursor.getString(0);
cursor.close();
}
}
Hope it´s useful!!
Thanks a lot is solved it this way:
protected void startCameraActivity() {
outputFileUri = Uri.fromFile(sdDir);
i = new Intent("android.media.action.IMAGE_CAPTURE");
i.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(i, 0);
}
//Manage everything that happens after the Camera was started
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//
// Write the Captured Image as File
Intent intent = new Intent();
intent.putExtra("uri", sdDir.getPath());
//Grab the Captured Image from the Cache an create the Preview
bmp = BitmapFactory.decodeFile(outputFileUri.getPath());
//Rotates the Preview Image
Matrix matrix=new Matrix();
matrix.postRotate(90);
Bitmap bMapRotate = Bitmap.createBitmap(bmp, 0, 0,bmp.getWidth(),bmp.getHeight(), matrix, true);
//Set the Rotated Image as Preview in the ImageView from the Layout
iv.setImageBitmap(bMapRotate);
setResult(0, intent);
}

How to share image from SDcard to gmail in android/ ANDROID?

I am doing R&D in this topic.
I am getting image from gallery and able to view in image view.
And by long press over that image view i can able to share.
But the problem is i am not getting the attached image as output..
public class Facebookhome extends Activity {
Button share;
ImageView img;
Uri screenshotUri;
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_facebookhome);
share = (Button) findViewById(R.id.button1);
img = (ImageView) findViewById(R.id.imageView1);
share.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select
Picture"),
SELECT_PICTURE);
}
});
img.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
shareimage();
return true;
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(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);
}
public void shareitem() {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "Here is the share content body";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
"Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
public void shareimage() {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
screenshotUri = Uri.parse(selectedImagePath);
sharingIntent.setType("image/jpg");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,
screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
// Toast.makeText(getBaseContext(), "FB Last",
// Toast.LENGTH_LONG).show();
}
}
private static int RESULT_LOAD_IMAGE = 1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
}
#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 cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
Uri screenshotUri = Uri.parse(picturePath);
cursor.close();
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("image/jpg"");
i.putExtra(Intent.EXTRA_EMAIL,
new String[] { "aa#gmail.com" });
i.putExtra(Intent.EXTRA_SUBJECT, "subject of email");
i.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(i, "Send mail..."));
}
}

How to get path to file using some external file managers apps Android?

Is it possible to get File from sd card, which is picked using some 3rd apps (file manager) ?
I mean that i have activity with button open file and when user press open it shows him suggestions to use some other app to open file, and when he pick open i get back to my activity with path of that file ?
use this
button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(intent, "Select Picture"),
SELECT_PICTURE);
}
});
and add two methods in your activity
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
String s1 = data.getDataString();
//String s1 = selectedImageUri.getPath();
Log.e("GetPath",s1);
//s1 = s1.replaceAll("file://","");
//Uri a = Uri.fromParts(s1,null,null);
Log.e("OK",""+selectedImageUri);
//Log.e("A",""+a);
selectedImagePath = getPath(selectedImageUri);
if(selectedImagePath==null && s1 != null)
{
selectedImagePath = s1.replaceAll("file://","");
}
// selectedImagePath = getPath(a);
Intent intent = new Intent(this, PhotoEditorActivity.class);
intent.putExtra("path", selectedImagePath);
startActivity(intent);
finish();
}
}
}
///////////////////////////////////
public String getPath(Uri uri) {
try{
String[] projection = { MediaStore.Images.Media.DATA };
Log.e("OK 1",""+projection);
Cursor cursor = managedQuery(uri, projection, null, null, null);
Log.e("OK 2",""+cursor);
if(cursor==null)
{
return null;
}
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
Log.e("OK 3",""+column_index);
cursor.moveToFirst();
Log.e("OK 4",""+cursor.getString(column_index));
return cursor.getString(column_index);
}
catch(Exception e)
{
Toast.makeText(PhotoActivity.this, "Image is too big in resolution please try again", 5).show();
return null;
}
}
and add this int as class member
private static final int SELECT_PICTURE = 1;
enjoy coading..

Categories

Resources