Open a default folder path in Android? - android

Issue:
Office is a folder in internal memory of Android.
Clicking a button in screen should always take one to default folder, Office.
Appreciate help as no accepted answers found.

In onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data == null)
return;
if (requestCode == viewfilerequestcode){
Uri Fileuri = data.getData();
String DFLocation="/mnt/sdcard/Office/";
String FilePath="";
if (FilePath.trim().equals(DFLocation.concat(GettheFilename(Fileuri))))
{
FilePath = DFLocation.concat(GettheFilename(Fileuri));
}
......
.......
........
}
.........
..........
}
Intent start:
btnview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent fileintent;
String[] mimestoview =
{"application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document", // .doc & .docx };
fileintent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
fileintent.setType("*/*");
fileintent.putExtra(Intent.EXTRA_MIME_TYPES, mimestoview );
fileintent.addCategory(Intent.CATEGORY_OPENABLE);
fileintent.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
fileintent.addFlags(FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivityForResult(fileintent, viewfilerequestcode);
} catch (ActivityNotFoundException e) {
lbl.setText("No activity to handle picking a file. Showing alternatives.");
}
}
});

Related

why it shows null on selecting a file from storage?

I want to choose a pdf file on a button click and send that file to a url on another buttonclick. My issue is with selecting the pdf file. I am getting null in "onActivityResult()" method.
My code is
SelectButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Pdf"), PDF_REQ_CODE);
}
});
ActivityResult method is
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PDF_REQ_CODE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uri = data.getData();;
SelectButton.setText("PDF is Selected");
}
}
But I am getting uri as null and getting null pointer exception when trying to get file path from uri.
Try this and tell me!
Button button = (Button) x.findViewById(R.id.buttonStripText);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
switch(requestCode){
case PICKFILE_RESULT_CODE:
if(resultCode==RESULT_OK){
fileName = data.getData().getPath();
}
break;
}
}

Pick Image from gallery works fine on emulator but not on device

Hello I am beginner in android. I am writing code to pick image from gallery. My code works fine on Nexus_S emulator with Lollipop 5.1.1, but not working on device (Motorola E2 with Lollipop 5.1.1)
Here is my code,
#Override
public void onClick(View v)
{
if(v.getId()==R.id.layout_pickImage)
{
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
intent.putExtra("return-data", true);
startActivityForResult(intent, Utilities.REQUEST_ADD_IMAGE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==Utilities.REQUEST_ADD_IMAGE)
{
if(data!=null)
{
final Bundle extras = data.getExtras();
if (extras != null)
{
try{
Uri uri = data.getData();
selected_img_bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
imgv_selected.setImageBitmap(selected_img_bitmap);
imgv_selected.setVisibility(View.VISIBLE);
}catch(Exception e)
{
Toast.makeText(this, "error "+e, Toast.LENGTH_SHORT).show();
}
}
}
}
}
Why do you need the extras? You are not using it anywhere. Just remove that extras and it should work.
The onActivityResult would look like this:
#Override
public void onActivityResult ( int requestCode, int resultCode, Intent data ) {
super.onActivityResult( requestCode, resultCode, data );
if ( requestCode == Utilities.REQUEST_ADD_IMAGE ) {
if ( data != null ) {
try {
Uri uri = data.getData();
selected_img_bitmap = MediaStore.Images.Media.getBitmap( getContentResolver(), uri );
imgv_selected.setImageBitmap( selected_img_bitmap );
imgv_selected.setVisibility( View.VISIBLE );
} catch ( Exception e ) {
Toast.makeText( this, "error " + e, Toast.LENGTH_SHORT ).show();
}
}
}
}
Let me know if it solves your problem

Select an mp3 file from SD card

I need to ask the user to select a media file from his SD card for playing. The following code doesn't work:
edit:
after I choose a mp3 file from the sd card folder I can't start him (play him). I think that the problem is that it doesn't entering to the "onActivityResult" function.
Intent i = new Intent();
i.setType("audio/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(i, RESULT_OK);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode==RESULT_OK)
{
Uri uri =data.getData();
if(uri!=null) {
try {
song.setDataSource(getApplicationContext(), uri);
song.prepare();
pl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
song.start();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
super.onActivityResult(CONTEXT_RESTRICTED, RESULT_OK, data);
}
}
Your question doesn't include definitions of some variables so I added my own and it played. Try this:-
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
...
Intent i = new Intent();
i.setType("audio/*");
i.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(i, 1);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
Uri uri = data.getData();
if (uri != null) {
try {
MediaPlayer song = new MediaPlayer();
song.setDataSource(getApplicationContext(), uri);
song.prepare();
song.start();
} catch (Exception e) {
}
}
super.onActivityResult(CONTEXT_RESTRICTED, RESULT_OK, data);
}
}
MediaPlayer Docs here

Get Image from the Gallery and Show in ImageView

I need to get an image from the gallery on a button click and show it into the imageview.
I am doing it in the following way:
btn_image_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getImageFromAlbum();
}
});
The method Definition is as:
private void getImageFromAlbum(){
try{
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}catch(Exception exp){
Log.i("Error",exp.toString());
}
}
The activity result method is
#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();
try {
bmp = getBitmapFromUri(selectedImage);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
image_view.setImageBitmap(bmp);
//to know about the selected image width and height
Toast.makeText(MainActivity.this, image_view.getDrawable().getIntrinsicWidth()+" & "+image_view.getDrawable().getIntrinsicHeight(), Toast.LENGTH_SHORT).show();
}
}
The Problem
The problem I am facing is when the image resolution is high suppose that if the image size is of 5mp to 13mp. It won't loads up and show up into the image view.
But the images with the low width and height are successfully loading into the image view!
Can somebody tell me any issues with the code and what I am doing wrong? I just want to import the camera images from the gallery and show them in the image view!
you can try this.
paste this code in your button click event.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, RESULT_LOAD_IMG);
and below code is your on activity result
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == RESULT_OK) {
try {
final Uri imageUri = data.getData();
final InputStream imageStream = getContentResolver().openInputStream(imageUri);
final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
image_view.setImageBitmap(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(PostImage.this, "Something went wrong", Toast.LENGTH_LONG).show();
}
}else {
Toast.makeText(PostImage.this, "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
I hope it is helpful for you.
I use this code:
This code used to start gallery activity.
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
And get the result in:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK)
switch (requestCode){
case GALLERY_REQUEST:
Uri selectedImage = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
carImage.setImageBitmap(bitmap);
} catch (IOException e) {
Log.i("TAG", "Some exception " + e);
}
break;
}
}
And don't forget to declare permission in AndroidManifest.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Because OnActivityResult method is deprecated, proper way nowadays with AndroidX Activity, is Activity Result APIs, and that recommended way, see docs
"registerForActivityResult() takes an ActivityResultContract and an ActivityResultCallback and returns an ActivityResultLauncher which you’ll use to launch the other activity."
ActivityResultLauncher<String> mGetContent = registerForActivityResult(new ActivityResultContracts.GetContent(),
new ActivityResultCallback<Uri>() {
#Override
public void onActivityResult(Uri uri) {
previewImage.setImageURI(uri);
}
});
And simply call
mGetContent.launch("image/*");
when needed.
startActivityForResult() is deprecated. Android introduced Activity Result Api for same purpose.
This is how to implement Activity Result
ActivityResultLauncher<Intent> imagePickerActivityResult = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
if (result != null) {
Uri imageUri = result.getData().getData();
Glide.with(this)
.load(imageUri)
.into(R.id.profileImageView);
}
}
}
);
Call above code by calling launch() with intent
Intent galleryIntent = new Intent(Intent.ACTION_PICK);
galleryIntent.setType("image/*");
imagePickerActivityResult.launch(galleryIntent);
I try all the solutions above but they don't work for me . I saw a code from tutorialspoint website and it works for me very well this is the code :
final int PICK_IMAGE = 100;
Button button = findViewById(R.id.button33);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent gallery = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(gallery, PICK_IMAGE);
}
});
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
Uri imageUri;
ImageView imageView = findViewById(R.id.image_main_galary);
if (resultCode == RESULT_OK && reqCode == 100){
imageUri = data.getData();
imageView.setImageURI(imageUri);
}
}
the most important issue : don't forget PERMISSION :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

camera activity does not return to the previous/caller activity

I read an text input from the user, this input i use it as a name of a picture i'm taking by the camera.
i store this name and the path of the image name into Sqlite Database.
what I'm trying to do is, after clicking OK to accept the taken picture, i want the saved path to be displayed in a toast.
the problem is, when I click OK to accept the picture, nothing is being displayed and i cant switch from the camera activity to the activity that called the camera activity"the previous activity"
Note: I'm using the emulator not a real device.
OnClickListener btn_TakePictureListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imgPath = retrievePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(imgPath));
startActivityForResult(intent, RequestCode);
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCode && resultCode == RESULT_OK) {
String s = data.getData().toString();
Toast.makeText(getBaseContext(), ""+s, Toast.LENGTH_SHORT).show();
}
}
if you are passing Uri for you image then you can retrieve image as taken by camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
// Set the file save path with directory
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
Uri imageuri= Uri.fromFile(picture);
//set to imageview here
}
}
EDIT:
For Getting data uri in onActivityResult start Camra Activiyt as:
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);
startActivityForResult(intent, 2);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
Uri uriimg = data.getData();
Toast.makeText(getBaseContext(), ""+uriimg.toString(), Toast.LENGTH_SHORT).show();
}
}

Categories

Resources