I am trying to open the gallery app from my android application. I want to just open the gallery app and not other apps with like fotos.
I have tried
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
and
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setType("image/*");
and
intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
The problem is that all this show a dialog where I can pick again from all apps that have images. So it shows a dialog with "Gallery", "Fotos", etc....
Is there a possibility to open just one app, without having to chose again?
EDIT: If you want to use the gallery API, which does show photos from all photo apps on the phone and which is not a standalone user-accessible app itself, you could use this code:
private static final int PICK_IMAGE = 100;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_picker);
imageView = (ImageView) findViewById(R.id.image_view);
Button pickImageButton = (Button) findViewById(R.id.pick_button);
pickImageButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
openGallery();
}
});
}
private void openGallery() {
Intent gallery =
new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode == PICK_IMAGE) {
Uri imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
}
Unfortunately, I don't believe choosing photos from a specific app is possible, as "the gallery app" that comes installed on devices varies by manufacturer and OS skin. For example, the default gallery on Pixel devices is actually Google Photos.
If I misunderstood, and you do want to simply launch the Gallery app, you can include the following code in your /src/[ActivityName].java file:
package com.example.andy.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
int view = R.layout.activity_main;
TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(view);
final LinearLayout parent = findViewById(R.id.parent);
textView = findViewById(R.id.text);
textView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.sec.android.gallery3d");
if (launchIntent != null) {
startActivity(launchIntent);
} else {
Toast.makeText(MainActivity.this, "There is no package available in android", Toast.LENGTH_LONG).show();
}
}
});
}
}
Notice that the app being opened is Gallery, the default Samsung photo gallery app. If you want to replace this with Google Photos, you would replace com.sec.android.gallery3d with com.google.android.apps.photos.
Related
I'm having a really hard time trying to figure out how to load an image when I have its URI.
The app works in the following manner:
I'm checking in the SQLite database whether a user exists and then I'm starting DescriptionActivity (code below). If the user had already been added to database I want to get the description from the database and load it into EditText (descFromDb[0] - no problem here) and get the URI from database in order to load the image into ImageView component (descFromDb[1] - lots of problems here), the logs from the Logcat are pasted below the class code. There's also some code for the case when user wants to upload a pic from the gallery and a button that updates the description and image URI in database.
I already added WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE and MANAGE_DOCUMENTS permissions to my manifest file however this still doesn't allow me to load the image based on its URI.
I read some threads that advised to use Intents and write code in a similar manner to how I set the image from gallery but tbh I don't really have a lot of experience in use of Intents other than starting a new Activity with layout added to it and those examples were quite confusing.
TL;DR I need to set the picture to ImageView while only having its URI - image.setImageURI(imgUri); doesn't work.
package com.example.kuba.myapplication;
import android.app.ActivityManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
import java.util.List;
import static com.example.kuba.myapplication.MainActivity.kidDatabase;
public class DescriptionActivity extends AppCompatActivity {
public static final int PICK_IMAGE = 1;
private static final String KIDS_DB = "kids_db";
private int kidId;
EditText descText;
Button imgBtn;
Button updateBtn;
ImageView image;
Uri imgUri = null;
Button refreshBtn;
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == PICK_IMAGE) {
imgUri = data.getData();
image.setImageURI(imgUri);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_desc);
kidId = getIntent().getIntExtra("KID_ID", -1);
image = (ImageView) findViewById(R.id.imageView);
imgBtn = (Button) findViewById(R.id.picBtn);
refreshBtn = (Button) findViewById(R.id.refreshBtn);
descText = (EditText) findViewById(R.id.descEditText);
final String[] descFromDb = new String[2];
new Thread(new Runnable() {
#Override
public void run() {
descFromDb[0] = kidDatabase.daoAccess().getDesc(kidId);
descFromDb[1] = kidDatabase.daoAccess().getImgPath(kidId);
if (descFromDb[0] != null) {
descText.setText(descFromDb[0]);
}
if (descFromDb[1] != null) {
imgUri = Uri.parse(descFromDb[1]);
image.setImageURI(null);
image.setImageURI(imgUri);
}
}
}).start();
refreshBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
image.setImageURI(imgUri);
}
});
imgBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}
});
updateBtn = (Button) findViewById(R.id.saveBtn);
updateBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new Thread(new Runnable() {
#Override
public void run() {
Kid kid = kidDatabase.daoAccess().fetchKidById(kidId);
if (kid == null) {
return;
} else {
String description = descText.getText().toString();
kidDatabase.daoAccess().updateDescAndPath(kidId, description, imgUri.toString());
}
}
}).start();
Toast.makeText(getApplicationContext(), "Desc updated", Toast.LENGTH_LONG).show();
}
});
}
}
04-28 15:02:23.264 16941-17042/com.example.kuba.myapplication I/WAR:: imguri: content://com.android.providers.media.documents/document/image%3A47
04-28 15:02:23.267 16941-17042/com.example.kuba.myapplication W/ImageView: Unable to open content: content://com.android.providers.media.documents/document/image%3A47
java.lang.SecurityException: Permission Denial: opening provider com.android.providers.media.MediaDocumentsProvider from ProcessRecord{fbe1f29 16941:com.example.kuba.myapplication/u0a84} (pid=16941, uid=10084) requires android.permission.MANAGE_DOCUMENTS or android.permission.MANAGE_DOCUMENTS
at android.os.Parcel.readException(Parcel.java:1684)
at android.os.Parcel.readException(Parcel.java:1637)
at android.app.ActivityManagerProxy.getContentProvider(ActivityManagerNative.java:4199)
at android.app.ActivityThread.acquireProvider(ActivityThread.java:5476)
at android.app.ContextImpl$ApplicationContentResolver.acquireUnstableProvider(ContextImpl.java:2239)
at android.content.ContentResolver.acquireUnstableProvider(ContentResolver.java:1517)
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1131)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:984)
at android.content.ContentResolver.openInputStream(ContentResolver.java:704)
at android.widget.ImageView.getDrawableFromUri(ImageView.java:900)
at android.widget.ImageView.resolveUri(ImageView.java:871)
at android.widget.ImageView.setImageURI(ImageView.java:490)
at android.support.v7.widget.AppCompatImageView.setImageURI(AppCompatImageView.java:115)
at com.example.kuba.myapplication.DescriptionActivity$1.run(DescriptionActivity.java:75)
at java.lang.Thread.run(Thread.java:761)
resolveUri failed on bad bitmap uri: content://com.android.providers.media.documents/document/image%3A47
04-28 15:02:23.378 16941-16946/com.example.kuba.myapplication I/art: Do partial code cache collection, code=60KB, data=59KB
After code cache collection, code=60KB, data=59KB
Increasing code cache capacity to 256KB
Just use an image loading library, for example Glide, add the dependency:
implementation "com.github.bumptech.glide:glide:4.7.1"
It must be compiled against compile SDK version v27 or higher. And it uses support library v27, if you want to use a lower version of the support library then refer here
And finally the usage:
Glide.with(this)
.load(url)
.into(imageView);
I am trying to fetch Image from Gallery. when user chooses a picture from Gallery, they need to ask crop the Image then the cropped image should be displayed in ImageView.
Here's what I tried:
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class Details extends AppCompatActivity {
ImageView i1,i2;
int num =2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details);
android.support.v7.app.ActionBar ab = getSupportActionBar();
if (ab != null) {
ab.hide();
}
i1 = (ImageView)findViewById(R.id.prof1);
i2 = (ImageView)findViewById(R.id.prof2);
i1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent img_uopload = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
img_uopload.putExtra("crop","true");
img_uopload.putExtra("aspectX",1);
img_uopload.putExtra("aspectY",1);
img_uopload.putExtra("outputX",200);
img_uopload.putExtra("outputY",200);
img_uopload.putExtra("return-data",true);
startActivityForResult(img_uopload,num);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == num && resultCode == RESULT_OK ){
if(data!=null){
Bundle extras = data.getExtras();
if(extras !=null){
Bitmap img = extras.getParcelable("data");
i1.setImageBitmap(img);
}
else{
Toast.makeText(getApplicationContext(),"Not Read",Toast.LENGTH_LONG).show();
}
}
}
}
}
Here the bundle value returns null.
In android manifest, I have included.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
UAC :
when user clicks image button,Gallery Opens
when user chooses image from gallery,He/She should ask to crop the image
Cropped Image should placed in ImageView
Currently I am not able to use any Github libraries.
After choosing an image user needs to crop the image as like this:
This is my screen:
You can use Crop intent check this :
try {
String picUri="your irl";
Intent myCropIntent = new Intent("com.android.camera.action.CROP");
myCropIntent.setDataAndType(picUri, "image/*");
myCropIntent.putExtra("crop", "true");
myCropIntent.putExtra("aspectX", 1);
myCropIntent.putExtra("aspectY", 1);
myCropIntent.putExtra("outputX", 128);
myCropIntent.putExtra("outputY", 128);
myCropIntent.putExtra("return-data", true);
startActivityForResult(myCropIntent, CROP_PIC_REQUEST_CODE);
}
catch (ActivityNotFoundException e) {
String errorMessage = "No Activity found";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
else you can also use this crop library simple-crop-image-lib
Rounded image view use this library RoundedImageView
I want to do speech to text conversion for one of my project. But I am not getting the desired result. Can you pls let me know what should I modify for the code to work?
package com.example.speechtotext;
import java.util.ArrayList;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.speech.RecognizerIntent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
protected static final int RESULT_SPEECH = 1;
private ImageButton btnSpeak;
private TextView txtText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtText = (TextView) findViewById(R.id.txtText);
btnSpeak = (ImageButton) findViewById(R.id.btnSpeak);
btnSpeak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(
RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, "en-US");
try {
startActivityForResult(intent, RESULT_SPEECH);
txtText.setText("");
} catch (ActivityNotFoundException a) {
Toast t = Toast.makeText(getApplicationContext(),
"Opps! Your device doesn't support Speech to Text",
Toast.LENGTH_SHORT);
t.show();
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
txtText.setText(text.get(0));
}
break;
}
}
}
}
There are no compilation or runtime errors. As soon as I click the "Speak Now" button on the emulator, it says that Oops!! Your device doesn't support speech to text. I tried to install the .apk in my android and check it.It throws a pop up msg that "Connection Problem" with a warning mark and two buttons on the bottom for "Speak again " and "cancel".
Pls tell me what is the problem.
public class VoiceRecognition extends Activity implements OnClickListener {
private static final int VOICE_RECOGNITION_REQUEST_CODE = 1234;
private ListView mList;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.voice_recognition);
Button speakButton = (Button) findViewById(R.id.btn_speak);
mList = (ListView) findViewById(R.id.list);
PackageManager pm = getPackageManager();
List<ResolveInfo> activities = pm.queryIntentActivities(new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH), 0);
if (activities.size() != 0) {
speakButton.setOnClickListener(this);
} else {
speakButton.setEnabled(false);
speakButton.setText("Recognizer not present");
}
}
public void onClick(View v) {
if (v.getId() == R.id.btn_speak) {
startVoiceRecognitionActivity();
}
}
private void startVoiceRecognitionActivity() {
Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Speech recognition demo");
startActivityForResult(intent, VOICE_RECOGNITION_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == VOICE_RECOGNITION_REQUEST_CODE && resultCode == RESULT_OK) {
// Fill the list view with the strings the recognizer thought it could have heard
ArrayList<String> matches = data.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
mList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, matches));
}
super.onActivityResult(requestCode, resultCode, data);
}
}
Your device needs to have the Intent which will be able to recognise voice. If such an Intent doesn't exist, you get the error you mentioned.
Try installing "Google Voice Search" app from the play store.
I am new to android and am trying to make an android app where the user can click an image and save it to a database . However i do not want the image to be stored locally in the gallery folder. Or everytime a picture is taken it saves itself in a self made directory on the phone and keeps replacing earlier pics .I donot want all pictures to be stored on the phone .
Below is my current code :
package com.example.camerastart;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CameraMainActivity extends Activity
{
private static final int CAMERA_PIC_REQUEST = 2500;
private Button cam_button;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_main);
cam_button = (Button) findViewById(R.id.button1);
cam_button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
// TODO Auto-generated method stub
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK)
{
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setImageBitmap(image);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_camera_main, menu);
return true;
}
}
any help will be appreciated
You can make a folder, and save all the images to this folder.
just remember to add .nomedia file to this folder, and then the gallery will not show those files
I'm integrating exact code from here: http://labs.makemachine.net/2010/03/simple-android-photo-capture/
How the activity should work: click "button" -> go to default camera. Take photo. "Retake" button works, "cancel" button works (brings back to "button" layout), but the "ok" button doesn't work (and then the image should appear above the "button" in the previous layout). Does this have something to do with how it's saving image to SD card? I can't figure it out! Also, I'm testing this app on a device.
Got it to work:
package com.android.xxx;
import java.io.File;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Window;
public class CameraView extends MenusHolder {
protected String _path;
protected boolean _taken;
protected static final String PHOTO_TAKEN = "photo_taken";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.create_event_view);
File imageDirectory = new File(Environment.getExternalStorageDirectory() + "/MyFolder/");
imageDirectory.mkdirs();
_path = Environment.getExternalStorageDirectory() + "/MyFolder/temporary_holder.jpg";
startCameraActivity();
}
protected void startCameraActivity() {
File file = new File(_path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode) {
case 0:
finish();
break;
case -1:
onPhotoTaken();
break;
}
}
protected void onPhotoTaken() {
_taken = true;
finish();
Intent newView1 = new Intent(CameraView.this, CreateEventView.class);
CameraView.this.startActivity(newView1);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(CameraView.PHOTO_TAKEN, _taken);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(CameraView.PHOTO_TAKEN)) {
onPhotoTaken();
}
}
}