Reputation: 141
I have spent much time trying to resolve this trivial problem but seem to be going in circles even after reading many tutorials and previously asked questions. I am a novice at programming but want to get better and believe that learning through mistakes is the best way.
Can someone please help identify the problem with this code and what can be done in order to allow the pictures taken by the device's camera to be saved onto the phone afterwards rather than being discarded and previewed.
Thanks in advance!
Java code for relevant class:
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Activity_Camera extends Activity implements View.OnClickListener {
public static final int cameraData = 1;
ImageButton ib;
ImageView iv;
Intent i;
Bitmap bmp;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Info:
Intent i = new Intent(this, Help.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
initialise();
}
private void initialise() {
iv = (ImageView) findViewById(R.id.ivPicReturn);
ib = (ImageButton) findViewById(R.id.ibTakePic);
ib.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//
i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
//
startActivityForResult(i, cameraData);
break;
}
}
//
private String getOutputMediaFileUri() {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
Intent imageIntent = null;
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
return null;
}
//
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}
}
}
Upvotes: 0
Views: 137
Reputation: 3814
because you're returning null return null;
in your getOutputMediaFileUri
try this instead
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
startActivityForResult(i, cameraData);
break;
}
}
private Uri getOutputMediaFileUri() {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image.jpg");
return Uri.fromFile(image);
}
Upvotes: 3