How to open an image file only using Built in gallery android? - android

Hi I want to Open a image only using built in Gallery in android. Now I'm using the following code, If i click the button it shows the menu which contains the third party tools installed to open the image. I want only built-in gallery, Is there any option to hide the other Third party tools are I can open directly with Gallery without showing the menu.
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);
}
}

Try to use
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PICTURE);

That's because on click of button you are passing intent in which you are setting its type intent.setType("image/*"); and intent.setAction(Intent.ACTION_GET_CONTENT); so it will show list of app installed on device.
If you want to open only gallery direct then you should pass intent for gallery only.

Related

android app exit when select image

I am blocked my app exit without any notification when I select images but when I select forum photo or gallery did not crashed.
I use creativesdk for photo edited.
screnshoot
package com.lamba.selfie;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.widget.ImageView;
import com.aviary.android.feather.sdk.AviaryIntent;
public class MainActivity extends AppCompatActivity {
private ImageView mResultImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mResultImageView = (ImageView) findViewById(R.id.resultImageView);
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,
"Select Picture"), 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
switch (requestCode) {
case 0:
Uri selectedImageUri = data.getData();
Uri imageUri = Uri.parse(getPath(selectedImageUri));
Intent imageEditorIntent = new AviaryIntent.Builder(this)
.setData(imageUri)
.build();
startActivity(imageEditorIntent);
/*case 1:
startActivityForResult(imageEditorIntent, 1);
Uri mImageUri = data.getData();
mResultImageView.setImageURI(mImageUri);
break;*/
}
}
}
public String getPath(Uri uri) {
// just some safety built in
if (uri == null) {
// TODO perform some logging or show user feedback
return null;
}
// try to retrieve the image from the media store first
// this will only work for images selected from gallery
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(uri, projection, null, null, null);
if (cursor != null) {
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
// this is our fallback here
return uri.getPath();
}
}
How could I fix it?
Source of the issue
You can simplify your switch statement's case 0 a bit and pass selectedImageUri directly to the AviaryIntent's setData() method.
As a result, you won't need your getPath() helper method.
Example code
Below is working code, based on your code above, that will:
Open the image source chooser immediately
Open the selected image in the Creative SDK Image Editor
Attach the edited image to the ImageView
See the comments in the example code to find where these things happen.
public class MainActivity extends AppCompatActivity {
private ImageView mSelectedImageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mSelectedImageView = (ImageView) findViewById(R.id.selectedImageView);
// Open the image source chooser immediately
Intent galleryIntent = new Intent();
galleryIntent.setType("image/*");
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent, "Select an image"), 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
// Open the Creative SDK Image Editor with the chosen image
case 0:
Uri selectedImageUri = data.getData();
Intent imageEditorIntent = new AviaryIntent.Builder(this)
.setData(selectedImageUri)
.build();
startActivityForResult(imageEditorIntent, 1);
break;
// Attach the edited image to the ImageView
case 1:
Uri editedImageUri = data.getData();
mSelectedImageView.setImageURI(editedImageUri);
break;
}
}
}
// ...
}
try to change true to false on your Activity in the manifest file.
android:noHistory="true" to android:noHistory="false"
your code : galleryIntent.setAction(Intent.ACTION_GET_CONTENT)
Use this : Intent GalleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);

Android MMS Intent with with Video file and body text

Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra("sms_body", "Hi how are you");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File("/sdcard/file.gif")));
intent.setType("image/gif");
startActivity(Intent.createChooser(intent,"Send"));
this code send mms with image and text.
But how send video file instead of image ?
I assume you want to pick your video from your memory. Then, firstly you have to import:
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import java.io.File;
This is how activity looks like when you want to import video from Gallery to MMS:
public class MMS extends Activity {
// Fields
private String TAG = "MMS";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// To display current window in full screen.
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/**
* See assets/res/layout/mms.xml for this view layout
* definition, which is being set here as the content of our screen.
*/
setContentView(R.layout.mms);
}
/*
* Open Gallery to select video to send on OnClick Event of someButton.
*/
public void onClickPicMMS(View view) {
Intent intent = new Intent();
intent.setType("video/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Video"),0);
}
/*
* Called when an activity you launched exits, giving you the requestCode you started it with, the resultCode it returned, and any additional data from it.
*/
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
// Fetch the path of selected image.
Uri uri = data.getData();
String[] projection = { MediaStore.Video.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
cursor.moveToFirst();
String mmsvideoPath = cursor.getString(column_index);
//Call the intent to send selected video as MMS.
Intent intent = new Intent(Intent.ACTION_SEND);
File f = new File(mmsvideoPath);
intent.putExtra("sms_body", "Hi how are you");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f)); // videoUri set previously
sendIntent.setType("video/3gp");
startActivity(intent);
}
}
}
Insert the permission in Manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET" />
try below code:-
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setClassName("com.android.mms", "com.android.mms.ui.ComposeMessageActivity");
sendIntent.putExtra("address", "9999999999");
sendIntent.putExtra("sms_body", "if you are sending text");
final File file1 = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"Downloadtest.3gp");
Uri uri = Uri.fromFile(file1);
Log.e("Path", "" + uri);
sendIntent.putExtra(Intent.EXTRA_STREAM, uri);
sendIntent.setType("video/3gp");
//sendIntent.setType("audio/3gp"); // sending audio
startActivity(sendIntent);

saving picture into drawable file or my data base android

i have this code .. which work perfectly when i click the button i will choose a picture from my device gallery and then view it on imageview
what i want to do is can i save the slected image into drawable file o my database ?
package com.example.testpic;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity
{
Button btnGal;
ImageView ivGalImg;
Bitmap bmp;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnGal = (Button)findViewById(R.id.btnGallary);
ivGalImg = (ImageView)findViewById(R.id.ivImage);
btnGal.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
openGallery();
}
});
}
private void openGallery()
{
Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultcode, Intent intent)
{
super.onActivityResult(requestCode, resultcode, intent);
if (requestCode == 1)
{
if (intent != null && resultcode == RESULT_OK)
{
Uri selectedImage = intent.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 filePath = cursor.getString(columnIndex);
cursor.close();
if(bmp != null && !bmp.isRecycled())
{
bmp = null;
}
bmp = BitmapFactory.decodeFile(filePath);
ivGalImg.setBackg`enter code here`roundResource(0);
ivGalImg.setImageBitmap(bmp);
}
else
{
Log.d("Status:", "Photopicker canceled");
}
}
}
}
you can not save images to your drawable folder after your apk file has been generated
you can save the path of image to your db
You can copy the image and past it to storage of your mobile programmatically, google for it

Print image location and put the image in image view

I am writing a android code to get the image location and after getting that put the physical image into a image view. SO far I have written code to open a popup window which allows user to select a file.
final int FILE_SELECT_CODE = 0;
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(Intent.createChooser(intent,
"Select a File to Upload"), FILE_SELECT_CODE);
} catch (Exception ex) {
Log.d("File upload", "error" + ex.toString());
}
Log.d("File Location", ":" + intent.getData().getPath());
I Tried this to get the location but it didnt show anything.
Now how do I get the file location and put it in a image view??
See this code for setting image in imageview.
package com.android.imagegalleray;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class ImageGalleryDemoActivity extends Activity {
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);
cursor.close();
ImageView imageView = (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
}

How to receive multiple gallery results in onActivityResult

I want to start the gallery via intent and want to get the results in my onActivityResult method.
All works fine if i click on one pic.
The question ive got now is, how do i get the results, if i choose more than one picture in the gallery?
If found hcpl´s code, which does it for one picture:
public class BrowsePicture extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button) findViewById(R.id.Button01))
.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);
}
You need to store that image path into array string then retrieve it according to your requirement.
Here is a sample code which will Pop up one Dialog box with 3 selection criteria like camera, Gallery, Device.
This may help you.
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
public class FileAttachPopUpUtils extends Activity {
private View rootView;
private Dialog popup;
private Button Assig_PopUp_DeviceBtn;
private Button Assig_PopUp_GalaryBtn;
private Button Assi_PopUp_CameraBtn;
private Button Assig_PopUp_CancelPopupBtn;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.assignment_popupfile);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
/*
* getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
* WindowManager.LayoutParams.FLAG_FULLSCREEN);
*/
popup = new Dialog(this);
// popup.requestWindowFeature(Window.FEATURE_NO_TITLE);
// popup.getWindow().setBackgroundDrawable(new
// ColorDrawable(android.graphics.Color.TRANSPARENT));
popup.setContentView(R.layout.assignment_popupfile);
popup.setCanceledOnTouchOutside(false);
popup.show();
Assig_PopUp_DeviceBtn = (Button) popup
.findViewById(R.id.Assignment_PopUp_DeviceBtn);
Assig_PopUp_GalaryBtn = (Button) popup
.findViewById(R.id.Assignment_PopUp_GalaryBtn);
Assi_PopUp_CameraBtn = (Button) popup
.findViewById(R.id.Assignment_PopUp_CameraBtn);
Assig_PopUp_CancelPopupBtn = (Button) popup
.findViewById(R.id.Assignment_PopUp_CancelPopupBtn);
Assig_PopUp_DeviceBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
/*
* Toast.makeText(FileAttachPopUpUtils.this,
* "Device File In-Progress", Toast.LENGTH_SHORT) .show();
*/
Intent intent = new Intent(FileAttachPopUpUtils.this,
FileExplore.class);
startActivity(intent);
popup.dismiss();
finish();
}
});
Assig_PopUp_GalaryBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
}
});
Assi_PopUp_CameraBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ///////////////////////////////////////////////////////////////
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
}
});
Assig_PopUp_CancelPopupBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
popup.dismiss();
FileAttachPopUpUtils.this.finish();
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == this.RESULT_OK) {
if (requestCode == 1) {
/*
* Intent intent = new Intent( Intent.ACTION_PICK,
* android.provider
* .MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
* startActivityForResult(intent, 2);
*/
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
// mImage.setImageBitmap(thumbnail);
// 3
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
// 4
String name;
int n = 100000;
int rand;
rand = new Random().nextInt(n);
name = "Image-" + rand + ".jpg";
// File fileimage = new File(path, name);
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + name);
System.out.println("FILE PATH=======>>>>>>>>>>"
+ file.toString());
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
// 5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Assignments.mulPathArry.add(file.toString());
// Here Your Storing the image Path each time it calls
popup.dismiss();
FileAttachPopUpUtils.this.finish();
} else if (requestCode == 2) {
Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = this.getContentResolver().query(selectedImage,
filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
System.out
.println("path of image from gallery......******************........."
+ picturePath);
Assignments.mulPathArry.add(picturePath);
// Here Your Storing the Image Path when each time it calls
popup.dismiss();
FileAttachPopUpUtils.this.finish();
}
}
// ///////////////////////////////////////////////////////////////////////
}
#Override
public void onBackPressed() {
return;
}
}

Categories

Resources