This question already has answers here:
Capture Image from Camera and Display in Activity
(19 answers)
Closed 5 years ago.
I am trying to display a image captured from camera inside imageview in android
Selecting a photo from the correct gallery works
But the camera fails: "app has stopped"
images.get(2).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
current_image = 2;
if (!fill_images[current_image]) {
show_dialog();
}else {
show_delete_dialog();
};
}
});
public void show_dialog() {
final ArrayList<String> list = new ArrayList<String>();
list.add("select from gallery");
list.add("take a photo");
AlertDialog.Builder builder = new AlertDialog.Builder(NewAdActivity.this);
builder.setAdapter(new ArrayAdapter<String>(NewAdActivity.this, R.layout.row, R.id.mytext, list)
, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
if (i == 0) {//gallery Intent gallery_intent=new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ; startActivityForResult(Intent.createChooser(gallery_intent,"select image"),2);
}
else if (i == 1){//camera
Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file=new File(Environment.getExternalStorageDirectory(),"file"+String.valueOf(System.currentTimeMillis()+".jpg"));
uri= Uri.fromFile(file);
camera_intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
camera_intent.putExtra("return-data",true);
startActivityForResult(camera_intent,1);
}
}
});
builder.show();
}
At first make sure that you declared all necessary permission on manifest.xml file. Then try with it -
public class MainActivity extends AppCompatActivity {
private static final int SELECTED_PICTURE=1;
private ImageView iv;
private Button btnClick;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
iv=(ImageView)findViewById(R.id.imageView1);
btnClick = (Button) findViewById(R.id.btnClick);
btnClick.setOnClickListener(new View.OnCLickListener(){
public void onClick(View view) {
btnClick(view);
}
});
}
public void btnClick(View v){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, SELECTED_PICTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(photo);
}
}
}
And dont forget to add permission to manifest.xml -
<uses-feature
android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
new Camera.PictureCallback() {
#Override
public void onPictureTaken(final byte[] data, Camera camera) {
bytes = data;
}
}
Then convert the bytes to bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
Then set imageview
imageView.setImageBitmap(bitmap);
Related
So, I want to take a picture and display it on the screen. I can press the button and take a picture but it isn't showing it on the ImageView. I can't find the solution with searching onActivityresult.
What I have now:
public class MainActivity extends AppCompatActivity {
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnCamera = findViewById(R.id.btnCamera);
imageView = findViewById(R.id.imageView);
// Open camera and take picture
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bitmap = (Bitmap)data.getExtras().get("Data");
imageView.setImageBitmap(bitmap);
}
}
Use this:
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
Instead of:
Bitmap bitmap = (Bitmap)data.getExtras().get("Data");
try this
if (requestCode == 0 && resultCode == RESULT_OK )
{
Uri imgUri = data.getData();
try {
Bitmap bm = MediaStore.Images.Media.getBitmap(getContentResolver(), imgUri);
imageView.setImageBitmap(bm);}}
I am cropping an image from the gallery but cannot able to set that cropped image in imageView, final image set as default image I mean without cropped so how to set Cropped image in ImageView
this is image Picker from gallery
newPostImg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
galIntent=new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(galIntent,"select image"),2);
}
});
This is for crop image
private void CropImage() {
try {
CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(postImageUri,"image/*");
CropIntent.putExtra("crop","true");
CropIntent.putExtra("outputX",180);
CropIntent.putExtra("outputY",180);
CropIntent.putExtra("aspectX",3);
CropIntent.putExtra("aspectY",4);
CropIntent.putExtra("scaleUpIfNeeded",true);
CropIntent.putExtra("return-data",true);
startActivityForResult(CropIntent,1);
}
catch (ActivityNotFoundException ex)
{
}
}
This is onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0&&resultCode ==RESULT_OK)
CropImage();
if (requestCode==2){
if (data!=null){
postImageUri=data.getData();
CropImage();
newPostImg.setImageURI(postImageUri);
}
}
}
And this is my permission
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA"/>
and this is imageView xml
<ImageView
android:layout_margin="10dp"
android:id="#+id/newPostImg"
android:layout_width="match_parent"
android:layout_height="250dp"
android:src="#drawable/default1" />
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == 2) {
if (data != null) {
postImageUri = data.getData();
CropImage();
}
} else if (requestCode == 1) {
Bundle extras = data.getExtras();
Bitmap bitmap= extras.getParcelable("data");
newPostImg.setImageBitmap(bitmap);
}
}
}
}
You should set Image on CropIntent's result and not on cameraIntent's result.
Uri imageUri = Uri.parse(mCurrentPhotoPath);
Intent intent = new Intent(mContext, Crop_Activity.class);
intent.putExtra("imageUri", imageUri.toString());
startActivityForResult(intent, 105);
Here is the code for crop activity
public class Crop_Activity extends BaseActivity implements CropImageView.OnSetImageUriCompleteListener, CropImageView.OnCropImageCompleteListener, View.OnClickListener {
private CropImageView mCropImageView;
private ProgressDialog dialog;
int type;
private Uri imageUri;
private Bitmap cropped;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.crop_activity);
setScreenName("Crop Image screen");
Bundle extras = getIntent().getExtras();
if (extras != null) {
imageUri = Uri.parse(getIntent().getStringExtra("imageUri"));
}
//====Initialize Here====//
init();
//====Set CropImageView Here====//
setCropImageView();
}
void init() {
dialog = new ProgressDialog(Crop_Activity.this);
dialog.setCancelable(false);
dialog.setMessage("Cropping Image...");
Func.set_title_to_actionbar("Crop Image", "", "", Crop_Activity.this, (Toolbar) findViewById(R.id.toolbar), true, 8, 8, Crop_Activity.this);
mCropImageView = (CropImageView) findViewById(R.id.CropImageView);
}
void setCropImageView() {
mCropImageView.setOnSetImageUriCompleteListener(this);
mCropImageView.setOnCropImageCompleteListener(this);
mCropImageView.setFixedAspectRatio(true);
mCropImageView.setImageUriAsync(imageUri);
mCropImageView.setShowProgressBar(false);
mCropImageView.setFixedAspectRatio(true);
mCropImageView.setAspectRatio(10, 10);
}
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.menu_crop, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
onBackPressed();
finish();
return true;
}
if (id == R.id.btn_ok) {
mCropImageView.getCroppedImageAsync();
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onSetImageUriComplete(CropImageView view, Uri uri, Exception error) {
}
#Override
public void onClick(View view) {
}
class CropingImageAsync extends AsyncTask<Void, Void, Void> {
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
Constants.Photo = cropped;
Intent i = new Intent();
setResult(RESULT_OK, i);
finish();
return null;
}
#Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
dialog.dismiss();
Intent intent = new Intent();
setResult(RESULT_OK, intent);
finish();
}
}
#Override
public void onCropImageComplete(CropImageView view, CropImageView.CropResult result) {
handleCropResult(result);
}
private void handleCropResult(CropImageView.CropResult result) {
if (result.getError() == null) {
Bitmap bitmap = result.getBitmap();
if (bitmap != null) {
cropped = bitmap;
if (cropped != null) {
new CropingImageAsync().execute();
}
}
} else {
Log.e("AIC", "Failed to crop image", result.getError());
}
}
}
Here is the XML for crop activity
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="#+id/line1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.theartofdev.edmodo.cropper.CropImageView
android:id="#+id/CropImageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:cropInitialCropWindowPaddingRatio="0" />
</LinearLayout>
Dependency for cropping
compile 'com.theartofdev.edmodo:android-image-cropper:2.3.+'
I am using this code to pick an image from gallery and display it in an imageView, the problem that this is working just for photos in internal storage, and when I pick an image from my sd card it's not displayed in my imageView
I hope you have a solution.
public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView) ;
btn = findViewById(R.id.button) ;
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
openGallery() ;
}
});
}
public void openGallery(){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
startActivityForResult(intent,PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode==PICK_IMAGE && data != null){
imageUri = data.getData() ;
img.setImageURI(imageUri);
}else{
Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
}
}
}
I am using those permissions in my manifest file :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE">
</uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
I asked and I will answer, the problem was that most of my sd card photos are 2.0 MB or more and they can't be treated directly by the imageView so I added some lines in onActivityResult()
public class MainActivity extends AppCompatActivity {
ImageView img ;
Button btn ;
Uri imageUri ;
private static final int PICK_IMAGE = 1 ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img = findViewById(R.id.imageView) ;
btn = findViewById(R.id.button) ;
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
openGallery() ;
}
});
}
public void openGallery(){
Intent intent = new Intent(Intent.ACTION_PICK,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI) ;
startActivityForResult(intent,PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK && requestCode==PICK_IMAGE && data != null){
imageUri = data.getData() ;
Bitmap bitmap ;
try {
bitmap=MediaStore.Images.Media
.getBitmap(this.getContentResolver(),imgUri);
Bitmap mBitmap = Bitmap.createScaledBitmap(bitmap, 160, 160, true);
img.setImageBitmap(mBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}else{
Toast.makeText(this," you haven't selected",Toast.LENGTH_LONG).show();
}
}
I hope this will be helpful for those who can face the same problem
I have created a fragment which a button and an image view in it. on button click the camera opens when I take a picture then I want that picture to appear in the imageView.
how can I activate this task?
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.fragment_new_visitor, container, false);
capture=rootview.findViewById(R.id.capture);
capture.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
}
});
return rootview;
please help
Just try this one,
Declare this before on create method
private final int requestCode = 20;
inside onclick event, do this
Intent photoCaptureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(photoCaptureIntent, requestCode);
then override oncreate method
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(this.requestCode == requestCode && resultCode == RESULT_OK){
Bitmap bitmap = (Bitmap)data.getExtras().get("data");
imageview.setImageBitmap(bitmap);
}
}
public class BlankFragment extends Fragment {
Button btnGallery,btnCamera;
public static final int GALLEY_REQUEST_CODE = 10;
private static final int CAMERA_REQUEST = 100;
private String TAG = MainActivity.class.getSimpleName();
ImageView image;
private Uri realUri;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
#Override
public void onStart() {
super.onStart();
btnGallery = (Button)getView().findViewById(R.id.btnGallery);
btnCamera = (Button)getView().findViewById(R.id.btnCamera);
image = (ImageView)getView().findViewById(R.id.image);
btnGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(openGallery, "Open Gallery"), GALLEY_REQUEST_CODE);
}
});
btnCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLEY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
image.setImageURI(data.getData()); // set image to image view
try{
// Get real path to make File
realUri = Uri.parse(getRealPathFromURI(data.getData()));
Log.d(TAG,"Image path :- "+realUri);
}
catch (Exception e){
Log.e(TAG,e.getMessage());
}
}
if(requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK){
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
image.setImageBitmap(photo);
}
}
}
public String getRealPathFromURI(Uri contentUri) {
String res = null;
String[] proj = { MediaStore.Images.Media.DATA};
Cursor cursor = getActivity().getApplicationContext().getContentResolver().query(contentUri, proj, null, null, null);
assert cursor != null;
if(cursor.moveToFirst()){;
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
res = cursor.getString(column_index);
}
cursor.close();
return res;
}
}
I feel sorry to come and ask people for help, but I've been stuck with this problem for a while and it's driving me mad.
I'm trying to take a button from MainActivity where it puts you into the camera and takes a picture. It then takes you to the next Activity where there is a preview of the very same image. But whenever I arrive to the next activity the image doesn't show up, just a placeholder. What am I doing wrong? Am I missing something in the code?
I've been looking around and tried to use Uri, but apparently it's not permitted anymore as you need to give specific permissions for the action, so FileProvider is recommended, but honestly It's a little bit too hard for a beginner like me. I tried and failed for hours.
MainActivity
public class MainActivity extends AppCompatActivity {
int CAPTURE_REQUEST;
Button button;
Bitmap thumbnail;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAPTURE_REQUEST);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_REQUEST) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
sendImage();
}
}
}
private void sendImage() {
Intent intent = new Intent(MainActivity.this, NextActivity.class);
intent.putExtra("image", thumbnail);
startActivity(intent);
}}
NextActivity
public class NextActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
ImageView imageView = (ImageView) findViewById(R.id.imageView);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_next);
Bundle extras = getIntent().getExtras();
if (extras != null) {
Bitmap image = (Bitmap) extras.get("image");
if (image != null) {
imageView.setImageBitmap(image);
}
}
}}
Change onActivityResult to
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == CAPTURE_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
sendImage();
}
}
You are overriding your global variable with the local declaration.