android :can't save image in folder and display it in activity - java

i want to capture an image and save it in folder, but when i capture it, my image is not save in folder that i want, but just save in gallery, and the name of my image is not like what i want.
my image also not display in my activity, is there anyone know, why this is happen?
this is my code :
private void dialogKamera() {
String sdCard= Environment.getExternalStorageDirectory()+"/android/data/spaj/spaj_foto.png";
File file=new File(sdCard);
if(file.exists())
file.delete();
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
image_spaj.setImageBitmap(photo);
}
i don't know where is my fault, i hope somebody can help me to solve my problem, thank you

You should make sure the output directory exists.
EDIT: removed spurious comment :-)
To ensure the directory exists:
File dir = file.getParentFile();
dir.mkdirs();
To display the image, here are several good resources:
How can I display image in Android Application,
Displaying simple bitmap on Android has a list of other resources,
And the tutorial Displaying Bitmaps in Your UI.

First make sure the directory exists, and then
try this:
Change path to
String sdCard= Environment.getExternalStorageDirectory()+"/Android/data/spaj/spaj_foto.png"
'Android' instead of 'android'.

Related

How to upload profile photo to app in Android Studio

I want the user to be able to access their gallery (by clicking the add photo ImageView), upload a photo of their choosing, and display that photo in the circular profile photo spot. I can't seem to find any definitive guides on how to do this. What is the easiest/best way to go about it? (in Java).
(I would have commented, but I canĀ“t, since I do not have 50 rep.)
You might wanna check:
Get Image from the Gallery and Show in ImageView
(Keep in mind that this is only about loading the Image, saving would need some extra)
Cheers!
you can use this to pick your photo:
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();
}
}

Open file from documents on Android Nougat

I'm using file helper class from this post https://stackoverflow.com/a/20559175/2281821 but it doesn't working properly in each case. I'm using Intent chooseIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT); to start file picker. I have Android Nougat and I'm receiving from onActivityResult uri: content://com.android.externalstorage.documents/document/home%3Aimage.png
What does this home: means? How can I get access to this file? I was trying with Environment.getExternalStorageDirectory() and System.getenv("EXTERNAL_STORAGE") but I can't get acess.
Following code can be used to get document path:
Intent galleryIntent = new Intent();
galleryIntent .setType("image/*");
galleryIntent .setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(galleryIntent , getString(R.string.app_name)),REQUEST_CODE);
OnActivityResult():
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE) {
Uri uri = data.getData();
}
}
}
You can use "uri" variable value to get bitmap. If you want to open document other image also then remove following line:
galleryIntent .setType("image/*");
You get access to a content scheme by opening for instance an InputStream on the uri.
InputStream is = getContentResolver().openInputStream(uri);
BitmapFactory.decodeFromStream(is) will happily read from your stream.

Where do i find an image's file path in MediaStore

I'm making an app that picks a image from the gallery and returns the result. I need to get the file path from that selection and store it for later use. I have looked at similar questions and apparently the file path of the image is stored in the column
MediaStore.Images.Media.DATA
How would anyone know that this is the file path? I've looked at the android documentation on MediaStore and it does not tell you where the file path column is. Any help would be appreciated.
Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, SELECT_PHOTO);
use this to get an image from gallery, it will return URI of the image which could be store store for later use
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != RESULT_CANCELED) {
if (requestCode == SELECT_PHOTO) {
Uri path = data.getData();
}
}
}

file chooser for android

I am making an android album app where I can create an album and add photos and delete photos from the album. Adding a photo is a bit tricky where I need a photo filename with a file path. This was very easy using JFileChooser in java but this is android and I have no clue on getting the filename and file path. Is there any thing in the android api where I can get the same functionality as the JFileChooser.
I am looking for a solution to this problem either using a file chooser of some sort or an entire to new approach. Any help is appreciated..
Or is there any other approach I can implement to add a photo...
You may use Intent.ACTION_PICK to invoke an image picker. This intent may be caught by the default gallery app, or some other app installed on the device.
private static final int REQUEST_PICKER = 1;
private void invokePicker() {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Complete action using"), REQUEST_PICKER);
}
Then receive the result on onActivityResult.
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
if (requestCode == PICK_FROM_FILE) {
// Get Uri of the file selected,
Uri theImageUri = data.getData();
// Or if you want a Bitmap,
Bitmap theBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), theImageUri);
}
}
Edited:
Though in this way you don't need a real file path, you can get it from MediaStore if you need.

How to select jpeg images rom android gallery?

I need to create a App where I open Android Gallery from my Android App and I should be able to view only .jpeg images in gallery, other images like .png or any other format should be removed .
Can someone please suggest how to do this ?
look this example for txt you can do the same with jpg files, just list the files and select only that you want to use. Android: File list in ListView.
NOTE: The folder for gallery is Enviroment.DIRECTORY_DCIM
Here is the code for that:
Intent intent = new Intent();
intent.setType("image/jpeg");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Main.this.startActivity(intent);
but not sure if all android galery apps would honor the type extra
Here you go...
// Directory path here
String path = "PATH/TO/GALLERY";
File folder = new File(path);
File[] directoryListing = folder.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
if (files.endsWith(".JPG") || files.endsWith(".JPEG")) {
// Handle your file here
}
}
}
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/jpg");
startActivityForResult(photoPickerIntent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = getContentResolver().openInputStream(selectedImage);
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
}
}
}

Categories