我的Android应用程序遇到了一些问题。我的想法是看到我使用我的应用拍摄的照片或从我的自定义图库中选择并查看它。首先我遇到查看图像的问题,特别是当我试图看到位图时我遇到空指针异常,然后当我试图看到来自图库的照片时,应用程序崩溃可能是因为我无法制作目录刷新正常。 这是我的代码:
`
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fotocamera);
b=(Button)findViewById(R.id.btnSelectPhoto);
//viewImage=(ImageView)findViewById(R.id.viewImage);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds options to the action bar if it is present.
getMenuInflater().inflate(R.menu.fotocamera, menu);
return true;
}
private void selectImage() {
final CharSequence[] options = { "Scatta una foto", "Scegli una foto dalla galleria", "Carica una foto", "Annulla" };
AlertDialog.Builder builder = new AlertDialog.Builder(FotocameraActivity.this);
builder.setTitle("Cosa vuoi fare?");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Scatta una foto")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
photoDirectory = new File(Environment.getExternalStorageDirectory()+ File.separator + "FTRRome");
File camdir = new File("/external/images/media");
boolean res = camdir.exists();
System.out.println(res);
if(!photoDirectory.exists()) {
photoDirectory.mkdirs();
}
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String currentDateandTime = sdf.format(new Date());
File f = new File(photoDirectory, (currentDateandTime+".jpg"));
lastPhoto = f;
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
System.out.println(photoDirectory.getAbsolutePath());
System.out.println(lastPhoto.getAbsolutePath());
startActivityForResult(intent, 1);
}
/*else if (options[item].equals("Scegli una foto dalla galleria")) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
System.out.println(Environment.getExternalStorageDirectory());
System.out.println(photoDirectory);
//sendBroadcast (new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory()+"/FTRRome/")));
}
else if (options[item].equals("Carica una foto dalla galleria")) {
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
System.out.println(Environment.getExternalStorageDirectory());
System.out.println(photoDirectory);
sendBroadcast (new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.parse("file://" + Environment.getExternalStorageDirectory()+"/FTRRome/")));
startActivityForResult(intent, 3);
}
else if (options[item].equals("Annulla")) {
dialog.dismiss();
}
*/
}
});
builder.show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
System.out.println("Questo è il codice di risultato: " + requestCode);
if (requestCode == 1) {
try {
System.out.println("Creo bitmap");
Bitmap bitmap;
System.out.println("Setto bitmap options");
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
System.out.println("Setto decodifica");
bitmap = BitmapFactory.decodeFile(lastPhoto.getAbsolutePath(),bitmapOptions);
System.out.println("Chiedo visualizzazione");
viewImage.setImageBitmap(bitmap);
OutputStream outFile = null;
try {
outFile = new FileOutputStream(lastPhoto);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
catch (Exception e) {
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
/*else if (requestCode == 2) {
Uri selectedImage = data.getData();
System.out.println(selectedImage.getPath());
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage, filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.w("path of image from gallery: ", picturePath+"");
viewImage.setImageBitmap(thumbnail);
}
*/
}
}
}`