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
Related
My app is able to take a photo and stores it into a specified folder.
Using ES File explorer, I'm able to confirm that the image has been stored in sd card and the specified folder.
However, my gallery is not showing this album immediately. It will need to take quite some time. Is there any ways that i can make the detection immediate using codes?
The following broadcasting codes did not seem to work. My gallery doesnt immediately do a refresh to retrieve the newly created album and image.
Kindly assist. Thanks!
if (type == MEDIA_TYPE_IMAGE){
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".png");
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(mediaFile);
mediaScanIntent.setData(contentUri);
v.getContext().sendBroadcast(mediaScanIntent);
}
It might take some time till it finished the scanning.
If you wish, you can at least be notified when it finished:
MediaScannerConnection.scanFile(
getApplicationContext(),
new String[]{file.getAbsolutePath()},
null,
new OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
// file was scanned
}
});
for more information read here.
I would like some help with saving pictures taken from my camera to a specific folder on the SD card. My camera opens up, takes the photos, and saves them; but it saves them to the standard folder. The code I have so for is:
public class Camera extends Activity {
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
String Path;
private Uri fileUri;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle extras=getIntent().getExtras();
Path= extras.getString("Path");
Log.d("camear","path: "+Path);
//File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "MyCameraApp");
Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
this.startActivity(intent);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
//mediaFile = new File(mediaStorageDir.getPath() + File.separator +"IMG_"+ timeStamp + ".jpg");
//fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
Uri outputFileUri= Uri.fromFile(new File(Path+"/camera/"+timeStamp+".jpg"));// create a file to save the image
intent.putExtra("output", outputFileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
The path is coming from another activity on the application, and it passes its values fine. In the previous activity, it creates the folders that I want to save the pictures into.
I have looked at the following answer, and tried to implement some of the suggestions: How to save images from Camera in Android to specific folder?. One thing I didnt try, was in the last suggestion on the OnActivityResult. Is that the key or is there somthing else I am missing? This page here mentions the ContentResolver The Camera Intent is simply not working, one thing is that both pages look like they want to do the same, but go about it in different ways.
I figured out what I was doing wrong. If you look at the code, I made two mistakes. The first mistake was with my path. I got the correct path from the previous activity, however I did not create a folder path here. This is the code that worked for me:
File Folder=new File(Path);
File picFile= new File(Folder.getPath()+"/"+timeStamp+".jpg");
The first line checks for the folder, the second line creates the file using the path to the folder. I didn't have either part. Without it finding the path, it was just saving were it wanted to.
Another issue that I had was with my Intent call, I used the: MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA. This without any #Overrides apparently acts like the actual camera saving in its folder. I changed this to:
MediaStore.ACTION_IMAGE_CAPTURE, which allows me a little more control.
Another issue that I Was having and figured out, was with the ACTION_IMAGE_CAPTURE, it would take and the close out. But if you implement onActivityResult using the result code returned from the camera application, you can either re-call the camera and make it run again, or you can close it out and go back to another activity. It was not part of the original question, but was an issue I was having.
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("_"));
I'm creating an app that downloads a lot of pictures from a website and stores them all in the cache folder so that it won't take up too much space on the phone.
Now my problem is that I want the user to be able to click on an image and it will load up the image in the default android picture viewer. I've researched and figured out how to do that no problem, but I'm not 100% sure if this method will work. I can call the Intent no problem and the Pictureviewer opens but it doesn't display the images?
Can anyone let me know if this is possible to do this way? Thanks
Here is the code calling the intent and getting the directory and files...
URL url = null;
try
{
url = new URL(assetsToFullScreen[arg2]);
} catch (MalformedURLException e)
{
e.printStackTrace();
}
File cacheDir = SingleArticle.this.getCacheDir();
String fileName = url.getFile();
fileName = fileName.substring(fileName.lastIndexOf('/') + 1);
File file = new File(cacheDir, fileName);
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(file),"image/png");
startActivity(i);*/
I have a following question. I'm using camera intent that starts from a menu button like this
case R.id.camera:
final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(getTempFile(this)));
startActivityForResult(intent, TAKE_PHOTO_CODE);
return true;
This is working just fine and it saves original image (original size which I want).
Than I also have the following code
private File getTempFile(LovneDobe lovneDobe) {
final File path = new File( Environment.getExternalStorageDirectory(), lovneDobe.getPackageName() );
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
if(!path.exists()){
path.mkdir();
}
return new File(path, "image.jpg");
}
and this code saves the picture on SDcard and it also puts it into my gallery. But the problem I'm facing is that it saves only one picture. When I take another one it overwrites the previous one.
And now my question is how can I modify this so that it would save every picture I would take?
Thank in advance to anyone who is willing to help.
I'm pretty sure It's because you are explicitly overriding the file. In your method "getTempFile", you always give the same path and name for every time you call the method. Try having a static counter, for example, to know how many pictures you have done, and in the return, put something like
return new File(path, "image"+counter+".jpg");