Created
August 13, 2013 13:20
-
-
Save PepeuCps/6220994 to your computer and use it in GitHub Desktop.
Exemplo de teste de como tirar uma fotografia, copiar para um diretório e apagar o original.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.app.Activity; | |
import android.content.ContentResolver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.database.Cursor; | |
import android.net.Uri; | |
import android.os.Bundle; | |
import android.provider.MediaStore; | |
import android.util.Log; | |
import android.view.View; | |
import java.io.BufferedInputStream; | |
import java.io.BufferedOutputStream; | |
import java.io.File; | |
import java.io.FileInputStream; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
public class TakePicture extends Activity { | |
//Variaveis | |
private static final int PICTURE_RESULT = 1001; | |
private String fileName = "seucaminho\foto_%s.jpg"; | |
private File fileResult; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.tirar_foto); | |
//Caminho da foto | |
fileName = String.format(fileName, System.currentTimeMillis()); | |
//Arquivo da foto | |
fileResult = new File(fileName); | |
} | |
@Override | |
public void onClick(View v) { | |
switch (v.getId()) { | |
case R.id.nova_foto: | |
Uri outputFileUri = Uri.fromFile(fileResult); | |
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); | |
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, outputFileUri); | |
startActivityForResult(cameraIntent, PICTURE_RESULT); | |
break; | |
} | |
} | |
@Override | |
public void onActivityResult(int requestCode, int resultCode, Intent intent) { | |
if (requestCode == PICTURE_RESULT) { | |
String path; | |
File tempfile; | |
path = getLastImagePath(this); | |
tempfile = new File(path); | |
copyFile(tempfile, fileResult); | |
tempfile.delete(); | |
} | |
} | |
/** | |
* Retorna o caminho da ultima imagem da media store | |
*/ | |
private String getLastImagePath(Context context){ | |
ContentResolver resolver = context.getContentResolver(); | |
final String[] imageColumns = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA }; | |
final String imageOrderBy = MediaStore.Images.Media._ID+" DESC"; | |
Cursor imageCursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageColumns, null, null, imageOrderBy); | |
if(imageCursor.moveToFirst()){ | |
String fullPath = imageCursor.getString(imageCursor.getColumnIndex(MediaStore.Images.Media.DATA)); | |
imageCursor.close(); | |
return fullPath; | |
}else{ | |
return null; | |
} | |
} | |
/** | |
* Copia o arquivo de um diretorio para outro | |
*/ | |
public boolean copyFile(File source, File dest) { | |
BufferedInputStream bis = null; | |
BufferedOutputStream bos = null; | |
try { | |
bis = new BufferedInputStream(new FileInputStream(source)); | |
bos = new BufferedOutputStream(new FileOutputStream(dest, false)); | |
byte[] buf = new byte[1024]; | |
bis.read(buf); | |
do { | |
bos.write(buf); | |
} while (bis.read(buf) != -1); | |
} catch (IOException e) { | |
Log.e("Tirar foto", "copyFile", e); | |
} finally { | |
try { | |
if (bis != null) bis.close(); | |
if (bos != null) bos.close(); | |
} catch (IOException e) { | |
Log.e("Tirar foto", "copyFile", e); | |
} | |
} | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment