I would like to launch the native android camera and save the image at a specified location. The problem is after I click the photo, the preview comes up with the options to Save/Discard. After I click save the camera launches again, and the image I captured is not saved in the specified location. Rather it gets saved in the default location. Actua开发者_JAVA百科lly I need the location of the image I clicked. Here's the code I use to launch the camera.
MediaScannerConnection_MSC = null;
String fileName = String.valueOf(System.currentTimeMillis())+".jpg";
f = new File(Environment.getExternalStorageDirectory(), fileName);
_imageUri = Uri.fromFile(f);
// create new Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
intent.putExtra(MediaStore.EXTRA_OUTPUT, _imageUri);
startActivityForResult(intent, 1);
Here's the code after returning from the camera
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// use imageUri here to access the image
final String imagePath = f.getAbsolutePath();
_MSC = new MediaScannerConnection(this, new MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
_MSC.scanFile(imagePath, null);
}
public void onScanCompleted(String path, Uri uri) {
_MSC.disconnect();
_MSC = null;
}
});
_MSC.connect();
}
}
}
what mistake am I doing here
I think I have your answer, I've searched high and low for this one, and it's kinda tricky.
First of all, I think on certain phones,
intent.putExtra(MediaStore.EXTRA_OUTPUT, _imageUri);
is ignored :-(
So my best answer for you is, don't set the "EXTRA_OUTPUT" flag, instead, let it run normally and return your new picture URI. Then, from there, use:
Uri u = intent.getData();
To get your new picture's info, then from there, I use a FileOutputStream to write the image file to the new location I want to have it to. Then, lastly, I delete the original file. :-)
I know it's kind of hokey, but it works, and I think it's pretty dependable. :-)
I hope that helps. :-)
-Jared
8-28-11
@llango J
Ok, well, here's the entire code, you're welcome to copy and paste it and see if you can get that to work :-)
For starters, you will want to call the camera like this:
startActivityForResult(CameraIntent, TAKE_PICTURE);
Then, for the result you will want this:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case TAKE_PICTURE:
Uri u = intent.getData();
//Toast.makeText(getApplicationContext(), u.getPath(), Toast.LENGTH_SHORT).show();
WriteFiles(u);
break;
}
}
}
And then the main magic happens in this function:
private void WriteFiles(Uri myNewPic) {
String TempFilePath;
String TempPath;
File directory = new File("/sdcard/" + getString(R.string.app_name));
if (!directory.exists()) {
directory.mkdirs();
}
String TempPictureFile;
try {
TempPictureFile = myNewPic.getLastPathSegment() + ".jpg";
TempFilePath = directory.getPath() + "/" + TempPictureFile;
FileOutputStream myOutStream = new FileOutputStream(TempFilePath);
InputStream myInStream = getContentResolver().openInputStream(myNewPic);
FileIO myFileIO = new FileIO();
myFileIO.copy(myInStream, myOutStream);
//now delete the file after copying
getContentResolver().delete(myNewPic, null, null);
TempFilePaths.add(TempFilePath);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), getString(R.string.ErrorOpeningFileOutput), Toast.LENGTH_SHORT).show();
}
}
I think that is all the parts you need in order to save using the native camera app. However, I must caution you, I eventually wound up abandoning this code because I needed more flexibility and dependability than this offered (I'm thinking if you use this app on a ton of different phones, I could see it having problems for one reason or another. Like for example, I've had a TON of problems as a result of Samsung customizing their UI, therefore things work great on all phones but the Samsung ones. :-P). So I wound up just creating a camera app of my own, but I'm sure depending on your application this might work just fine for you. Either way.... good luck! :-)
Ok, here's my FileIO class that I use, sorry I didn't include this initially:
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.util.Log;
public class FileIO {
private static final int BUFFER_SIZE = 1024 * 2;
public FileIO() {
// Utility class.
}
public int copy(InputStream input, OutputStream output) throws Exception, IOException {
byte[] buffer = new byte[BUFFER_SIZE];
BufferedInputStream in = new BufferedInputStream(input, BUFFER_SIZE);
BufferedOutputStream out = new BufferedOutputStream(output, BUFFER_SIZE);
int count = 0, n = 0;
try {
while ((n = in.read(buffer, 0, BUFFER_SIZE)) != -1) {
out.write(buffer, 0, n);
count += n;
}
out.flush();
} finally {
try {
out.close();
} catch (IOException e) {
Log.e(e.getMessage(), null);
}
try {
in.close();
} catch (IOException e) {
Log.e(e.getMessage(), null);
}
}
return count;
}
}
精彩评论