当我尝试调用Camera Intent并让它返回我拍摄的照片的URI并将其保存在Sqlite数据库中时,我遇到了问题。
我总是得到以下日志:
06-06 13:06:35.195 19091-19091/com.example.eduardo.voiceblog_beta >E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.eduardo.voiceblog_beta, PID: 19091 java.lang.NullPointerException: file
at android.net.Uri.fromFile(Uri.java:447)
at camera.Util.getOutputMediaFileUri(Util.java:21)
at >com.example.eduardo.voiceblog_beta.ConfirmaFoto$1.onClick(ConfirmaFoto.ja>va:64)
at android.view.View.performClick(View.java:4761)
at android.view.View$PerformClick.run(View.java:19767)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at >com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.jav>a:901)
at >com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
这是我的Util类:
package camera;
import android.net.Uri;
import android.os.Environment;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Created by eduardo on 31/05/15.
* Classe onde defino caminho onde as fotos serão armazenadas
*/
public class Util {
public static final int TIPO_IMAGEM = 1;
public static String localFoto;
public static Uri getOutputMediaFileUri (int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/*public static Uri getOutputMediaFileUri (int type) {
return Uri.fromFile(getOutputMediaFile(type));
}*/
public static File getOutputMediaFile (int type){
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), "TesteCamera");
if (! mediaStorageDir.exists()) {
if (! mediaStorageDir.mkdirs()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
if (type == TIPO_IMAGEM) {
localFoto = mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg";
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG_"+ timeStamp + ".jpg");
} else {
return null;
}
return mediaFile;
}
}
这是我称之为照片意图的课程:
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import java.text.SimpleDateFormat;
import java.util.Date;
import BD.Postagem;
import BD.PostagemDAO;
import camera.Util;
public class ConfirmaFoto extends ActionBarActivity {
Button btnProximo;
private PostagemDAO bd;
private static final int TIPO_IMAGEM = 1;
String uriFoto;
private Uri fileUri;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_confirma_foto);
btnProximo = (Button) findViewById(R.id.btnConfirmaFoto);
btnProximo.setOnClickListener(proximo);
bd = new PostagemDAO(this);
bd.abrir();
}
View.OnClickListener proximo = new View.OnClickListener() {
public void onClick(View v) {
Intent principal = new Intent(ConfirmaFoto.this, MainActivity.class);
String tit = getIntent().getExtras().getString("titulo",null);
String com = getIntent().getExtras().getString("comentario",null);
Postagem postagem = new Postagem();
postagem.setTituloPostagem(tit);
postagem.setComentarioPostagem(com);
Date dataAtual = new Date();
SimpleDateFormat formataData = new SimpleDateFormat("dd/MM/yyyy");
String data = formataData.format(dataAtual);
postagem.setDataPostagem(data);
postagem.setExtra("FIXO");
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = Util.getOutputMediaFileUri(Util.TIPO_IMAGEM);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, 1);
postagem.setCaminhoFoto(uriFoto);
bd.criarPostagem(postagem);
bd.fechar();
startActivity(principal);
}
};
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
uriFoto = Util.getOutputMediaFileUri(Util.TIPO_IMAGEM).toString();
/*Bitmap vt = BitmapFactory.decodeFile(Util.localFoto);
vt = Bitmap.createScaledBitmap(vt, 480, 600, false);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap resizedBitmap = Bitmap.createBitmap(vt, 0, 0, 480, 600,matrix, true);
img.setImageBitmap(resizedBitmap);*/
} else if (resultCode == RESULT_CANCELED) {
} else {
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_confirma_foto, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
答案 0 :(得分:1)
我认为问题在于:
if (! mediaStorageDir.exists()) {
if (! mediaStorageDir.mkdirs()) {
return null;
}
}
您需要将此权限添加到清单:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>