I'm trying to allow user to rename file name, after taking picture, but I need to allow renaming to it right after saving the picture. So I want the name of the image to be shown in an EditText.
My saving code:
Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri mUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "/Folder/cam_"+ String.valueOf(System.currentTimeMillis()) + ".jpg"));
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(takePictureFromCameraIntent,1111);
String oriFileName = mUri.getPath();
My plan is : get the path with the name (oriFileName), then remove the front leave only the file name(cam_ ). But I have no idea on how to do so.
here you go:
String oriFileName = mUri.getPath();
oriFileName = oriFileName.substring(0, oriFileName.lastIndexOf("_"));
Related
I am making a photo app in which in the first activity the user takes a picture (he can see the picture in the ImageView), in the second activity he chooses with who to share the image,and in the 3rd activity he should be able to see the image again in a different ImageView than the first to add some data. I know how to move the bitmap from one activity to the next one by an intent, but how to do it if i want to send it to the 3rd activity of my user path? If i startActivity(intent) it will skip my second activity and if i don´t put it the 3rd activity is showing me an empty ImageView.. Can someone please help me in telling me ways of how to automatically load (without user interaction) this picture in the 1st and 3rd activity and some example?
I already being reading posts about how to convert to Base64 and load again, but their examples are using images already in the memory of the phone and in my case are pictures that were just taken by the user, so in principle i don´t know the name of the image file..
Thank a lot!
Add This Image In Your Custome Catch Folder
Like Make Your Folder in External or Internal Storage
Then Save Image that will capture by camera inside That Folder..
public static void SaveImagecatch(Bitmap finalBitmap) throws IOException {
File Folder = new File(Environment.getExternalStorageDirectory() + "/data/Catch");
if (Folder.mkdir()) {
nomediaFile = new File(Environment.getExternalStorageDirectory() + "/data/Catch/" + NOMEDIA);
if (!nomediaFile.exists()) {
nomediaFile.createNewFile();
}
}
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/data/Catch");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-" + n + ".jpg";
File file = new File(myDir, fname);
Catch_uri = Uri.parse("file://" + myDir + "/" + fname);
if (file.exists()) file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
Log.e("yes", "yes");
} catch (Exception e) {
e.printStackTrace();
Log.e("no", "no");
}
}
Then.. get Image From Uri path of Your saved Image.
Uri imageUri = Catch_uri;
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),imageUri);
Imageview my_img_view = (Imageview ) findViewById (R.id.my_img_view);
my_img_view.setImageBitmap(bitmap);
This is Worked For Me.. I Hope This Will be Helpfull to you
Actually in second activity, you need to get Intent from first activity and do your work and then create new Intent and put your image into it, finally start third activity using new intent.
in second activity:
Intent firstToSecodeIntent = getIntent();
// some codes
Intent secondToThirdIntent = new Intent(this, ThirdActivity.class);
Intent.putExtra("image", /*your Image object*/);
startActivity(secondToThirdIntent);
in third activity:
Intent secondToThirdIntent = getIntent();
// get your image and set it into your imageView
when i use this code ->
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
I am able to save the image to specified path but it is also saving to the gallery. I dont want to save the image to gallery. Please help here.
Thanks in advance for your valuable time.
try this
private void captureCameraImage() {
Intent chooserIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
chooserIntent.putExtra(MediaStore.EXTRA_OUTPUT, getFilename());
startActivityForResult(chooserIntent, CAMERA_PHOTO);
}
method to return file name that you can specify whre you want to save
public String getFilename() {
File file = new File(Environment.getExternalStorageDirectory().getPath(), "MyFolder/Images");
if (!file.exists()) {
file.mkdirs();
}
String uriSting = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg");
return uriSting;
}
Capture Image using below Intent -
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
// create a file to save the image
File MyDir = new File(Environment.getExternalStorageDirectory() + File.separator + "MyDir");
if (!MyDir.exists()){
MyDir.mkdirs()
}
File fileUri = new File(MyDir.getAbsolutePath() + File.separator + "IMG_"+ timeStamp + ".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
The gallery app scans the folders for Media contents and populates them in gallery.
If you wish not to display your captured images in gallery follow below methods-
Creating A .Nomedia File
Adding A Dot Prefix
I faced the same problem and implemented several workarounds similar to this one.
I tried also to keep the file hidden adding the . prefix to the filname and to put a .nomedia file (see MediaStore.MEDIA_IGNORE_FILENAME) within the folder where I stored the images but in some cases, calling the camera app via intent as usual
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
Uri fileUri = FileProvider.getUriForFile(getContext(), getString(R.string.file_provider_authority), file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
} else {
showToastMessage(getString(R.string.no_camera_activity), Toast.LENGTH_LONG);
}
depending on the device and the camera app, this latter might store the picture also within the gallery (usually saving a file with a timestamp as the filename) even if you are providing an Uri associated to a file you are storing within your application private partition.
So I found that the most reliable way of doing what I needed was to control the camera directly by your own or to adopt cwac-cam2 library provided by CommonsWare within YourActivity in this way (note the commented .updateMediaStore() line)
Uri fileUri = FileProvider.getUriForFile(getContext(), getString(R.string.file_provider_authority), file);
CameraActivity.IntentBuilder builder = new CameraActivity.IntentBuilder(this); // this refers to the activity instance
Intent intent = builder
.skipConfirm()
.facing(Facing.BACK)
.to(fileUri)
//.updateMediaStore() // uncomment only if you want to update MediaStore
.flashMode(FlashMode.AUTO)
.build();
startActivityForResult(intent, CAMERA_REQUEST_CODE);
My app intends to launch a file using ACTION_VIEW.
The following code returns the file path of the selected file
if(Intent.ACTION_VIEW.equals(action)){
String Path = intent.getDataString();
//file processing code
}
It works fine when the selected file has no spaces in it. e.g Path becomes "/mnt/sdcard/sample.pdf" , but when i select a file with spaces in it's name such as "/mnt/sdcard/4C 1099 + 2 WOOO6.pdf" Path becomes "/mnt/sdcard/4C%20%20%201099%20%20%20%2B%20%202%20W0006.pdf"
Any help?
if(Intent.ACTION_VIEW.equals(action)){
Uri uri = intent.getData();
path = uri.getPath();
path = path.replace("%20", " ");
}
I am using an Intent to let the user select a file, and after the user has done that I want to know what kind of file-type the selected file is.
The intent:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
In my onActivityResult I want to extract the path from the intent through intent.getData() and all of it's "submethods" (getScheme(), getLastPathSegment() etc). However, I only get the Uri for the selected file.
Example of Uris':
Uri: content://media/external/audio/media/15185 //This is an audiofile
Uri: content://media/external/audio/media/20 //Another audiofile
Uri: content://media/external/images/media/20577 //this is a picture
Uri: file:///storage/emulated/0/testWed%20Apr%2017%2011%3A10%3A34%20CEST%202013 //This is a file
I've seen solutions of how to get the absolute path when the user is only allowed to chose images or audios. But how do I do I get the absolutePath (the real path with the name and file-ending, e.g. MyPicture.jpeg) if I want to allow the user to select from different file types?
The code I've been twiggling with to try to get the path-name in onActivityResult(int requestCode, int resultCode
String fileName = data.getData().getLastPathSegment().toString();
System.out.println("Uri: " +data.getData().toString());
File f = new File(fileName);
System.out.println("TrYinG wIWThA Da FiLE: " +f.getAbsolutePath());
System.out.println("FileNAME!!!: "+fileName);
By file-ending I'm assuming you mean the file extension? (i.e. mp4)
If so you can use the ContentResolver to get the mimetype:
String mimeType = getContentResolver().getType(sourceUri);
This will give you the mimetype assuming they are within the MediaStore which they should if you have the content Uri. so you'll get something like "image/png", "video/mp4", etc.
Then you can use MimeTypeMap to get the file extension from the mimetype:
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
Note that you should never substring the mimetype after "/" to get the extension as some mimetype does not use its extension such as ".mov" files which has the mimetype "video/quicktime"
How would I go about saving an image to a folder within the app? I want to eventually allow users to take pictures and upload and allow others to "rate it." I'm new to android, so I'm sorry if this is very basic. This is what I have so far.
public void onClick(View v) {
// TODO Auto-generated method stub
Intent picture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//MainActivity.this.startActivity(picture);
startActivityForResult(picture, 1);
onActivityResult(1, 1, picture){
}
}
Have you tried saving it elsewhere and then attempting to move it?
You could write a file in which saves it to its original directory and then will move into the one that you wish to do automatically.
You obviously already have enough skills to relocate a file within that executable.
Sorry if im not much help im brand new to the whole stackoverflow community.
hope i helped you though!
read this tutorial: http://developer.android.com/training/camera/photobasics.html
hope this will help you..
Use this piece of code. Explanations are after the code
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"YOUR_FOLDER_NAME/");
else
cameraFolder= StatusUpdate.this.getCacheDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'T'HHmmss");
String timeStamp = dateFormat.format(new Date());
String imageFileName = "picture_" + timeStamp + ".jpg";
File photo = new File(Environment.getExternalStorageDirectory(), "YOUR_FOLDER_NAME/" + imageFileName);
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);
startActivityForResult(getCameraImage, 1);
What this code essentially does is:
Creates a folder with the name provided where it reads
YOUR_FOLDER_NAME (Change this to your convenience)
The picture_" + timeStamp + ".jpg ensures that multiple images will
be stored in the folder of your choice, each with a new timestamp.
Naturally, the timestamp will be the current time.
The initialURI is globally defined to help you process the Image
taken later. For example, displaying it in an ImageView