I'm making an android application, when clicking on a button it opens the camera however after taking the picture the image doesn't save. I want to be able to save it into my gallery album.
public void onClickbtnCamera(View v){
Intent intent1 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uriSavedImage=Uri.fromFile(new File("/storage/emulated/0/DCIM/Camera/"));
intent1.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent1,3);
}
You are passing in a directory in EXTRA_OUTPUT. EXTRA_OUTPUT needs to point to a file:
package com.commonsware.android.camcon;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;
public class CameraContentDemoActivity extends Activity {
private static final int CONTENT_REQUEST=1337;
private File output=null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output=new File(dir, "CameraContentDemo.jpeg");
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(i, CONTENT_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == CONTENT_REQUEST) {
if (resultCode == RESULT_OK) {
Intent i=new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(output), "image/jpeg");
startActivity(i);
finish();
}
}
}
}
(from this sample project)
Related
I'm trying to integrate Zxing into my android application so the user can scan a qr code and it returns the contents of the QR code. I am able to open the barcode scanner however although it looks like it's doing something it doesn't scan the QR code. I have tested it on bar codes and it works so it looks like the issue is specific to qr codes. I've included some code snippets below.
Manifest File
<activity
android:name="com.journeyapps.barcodescanner.CaptureActivity"
android:screenOrientation="portrait"
tools:replace="android:screenOrientation"
android:stateNotNeeded="true"/>
QR scanner fragment
package com.example.ntuevent.ui.qrScanner;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import com.example.ntuevent.R;
import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
public class QRScannerFragment extends Fragment implements View.OnClickListener {
private QRScannerViewModel qrScannerViewModel;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
qrScannerViewModel =
ViewModelProviders.of(this).get(QRScannerViewModel.class);
View root = inflater.inflate(R.layout.fragment_qr_scanner, container, false);
final TextView textView = root.findViewById(R.id.text_qr_scanner);
qrScannerViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
root.findViewById(R.id.qr_scanner_button).setOnClickListener(this);
return root;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.qr_scanner_button:
/* Request camera access */
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, 1);
launchQrScanner();
}
}
private void launchQrScanner() {
if (validateCameraPermission()) {
/* Start the scanner */
IntentIntegrator intentIntegrator = new IntentIntegrator(getActivity());
/* Customisation options */
intentIntegrator.setDesiredBarcodeFormats(IntentIntegrator.ONE_D_CODE_TYPES);
intentIntegrator.setPrompt("Scan a barcode");
intentIntegrator.setCameraId(0); // Use a specific camera of the device
intentIntegrator.setBeepEnabled(false);
intentIntegrator.setOrientationLocked(true);
/* Start QR scanner */
intentIntegrator.initiateScan();
}
}
private boolean validateCameraPermission() {
/* Validates if app has access to camera */
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getContext().getApplicationContext(), "Enable camera permissions to access this feature", Toast.LENGTH_SHORT).show();
return false;
}
return true;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (intentResult != null) {
if (intentResult.getContents() == null)
Toast.makeText(getContext().getApplicationContext(), "Scan was cancelled", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getContext().getApplicationContext(), intentResult.getContents(), Toast.LENGTH_SHORT).show();
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
}
Any help would be great!
do like me for QR code scanner, I used this code for 2 projects and worked without any problem.
first, add below libraries to your Gradle:
implementation 'me.dm7.barcodescanner:zxing:1.9.13'
implementation 'com.journeyapps:zxing-android-embedded:3.6.0#aar'
implementation 'com.google.zxing:core:3.3.3'
second, in your QR code scanner activity add below codes:
private IntentIntegrator qrScan;
in onCreate add below:
qrScan = new IntentIntegrator(this);
qrScan.setOrientationLocked(false);
qrScan.initiateScan();
after onCreate add below:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode,
resultCode, data);
if (result != null) {
if (result.getContents() == null) {
//scan have an error
} else {
//scan is successful
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
package com.example.sugandhabansal.gallerypickimage;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private static final int SELECTED_PIC = 1;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
}
public void btnClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECTED_PIC);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECTED_PIC:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String filepath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(filepath);
Drawable drawable = new BitmapDrawable(Resources.getSystem(), bitmap);
imageView.setBackground(drawable);
}
break;
default:
break;
}
}
}
This is the MainActivity.java file. The code basically loads a image from gallery and displays it in imageview section of the layout.
When I ran the apk on my smartphone, no image was seen in imageview section after selection and after debugging it was seen that bitmap is equal to null.
Can someone please help me with this problem as I'm unable to solve it.
Make sure the obtained file path is a valid path.
Make sure you have the permissions to access storage.
If above things are okay and you still get null result, try following code:
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
I'm new to Android. I want to create a directory in my app. I will only display names there. I'm using Directory class to display names and AddPerson class to add there. However AddPerson is not working. Here is my Directory:
package com.example.user.myapplication;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import java.util.ArrayList;
public class Directory extends AppCompatActivity {
ArrayList<String> personList = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
ListView list;
ImageButton addButton;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_directory);
list = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1, personList);
list.setAdapter(adapter);
addButton = (ImageButton)findViewById(R.id.imageButton5);
addButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.example.user.myapplication.AddPerson");
startActivityForResult(intent, 1);
}
}
);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ( requestCode == 1 && resultCode == RESULT_OK ){
String result=data.getStringExtra("result");
personList.add(result);
}
}
}
This is the AddPerson class:
package com.example.user.myapplication;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class AddPerson extends AppCompatActivity {
Button addButton;
EditText nameText;
Directory d = new Directory();
public String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_person);
addButton = (Button) findViewById(R.id.button);
addButton.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
nameText = (EditText) findViewById(R.id.editText);
EditText mailText = (EditText) findViewById(R.id.editText2);
name = nameText.getText().toString();
Intent returnIntent = new Intent();
returnIntent.putExtra("result", name);
setResult(1, returnIntent);
finish();
}
}
);
}
}
This if is always false because you are returning 1 as the resultCode:
if ( requestCode == 1 && resultCode == RESULT_OK )
You need to return the correct result code with setResult(RESULT_OK, returnIntent);
On top of that, each time you modify your dataset you need to notify the ListView's adapter that it has changed. Move your adapter to a class variable and call adapter.notifyDataSetChanged(); after adding the resulting person.
Well, the main thing that sticks out is that you're not using RESULT_OK (or RESULT_CANCELED), maybe try:
public void onClick(View v) {
nameText = (EditText) findViewById(R.id.editText);
EditText mailText = (EditText) findViewById(R.id.editText2);
name = nameText.getText().toString();
Intent returnIntent = new Intent();
returnIntent.putExtra("result", name);
setResult(Activity.RESULT_OK, returnIntent);
finish();
Also, why are you manually creating a Directory (which is an Activity in this context) object in your AddPerson Activity? The system is the only thing that should create Activities.
Use setResult(Activity.RESULT_OK, returnIntent);
remember that
int RESULT_OK has a value of Constant Value: -1 (0xffffffff)
then you will have no problems to add data to the list:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ( requestCode == 1 && resultCode == RESULT_OK ){
String result=data.getStringExtra("result");
personList.add(result);
}
}
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);
}
}
}
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);
}
here is my code to convert the picture taken from the camera into text using tesseract's java wrapper tess4j! i have included the jar files added them to my path! code does not show any syntax errors! i launch the default camera it starts i take picture and it is shown in the image view i declared however the editbox seems empty though it should show the result of OCR!
package your.apnakaam.namespace;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.Bitmap;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.File;
import java.io.File;
import net.sourceforge.tess4j.*;
public class KaamsekhaActivity extends Activity
{
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
//**************************************************************************************************
private Intent data;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setContentView(R.layout.apna_layout);
Button capt_but = (Button)findViewById(R.id.capture_btn);
this.imageView = (ImageView)this.findViewById(R.id.picture);
capt_but.setOnClickListener(new View.OnClickListener()
{
//#Override
// TODO Auto-generated method stub
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
void myfunction()
}
}
public void myfunction()
{
TextView disp = (TextView)findViewById(R.id.editText1);
File new_pic = (File) data.getExtras().get("data");
Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
try
{
String result = instance.doOCR(new_pic);
disp.setText(result);
}
catch (TesseractException e)
{
System.err.println(e.getMessage());
}
}
}
I can't figure out whats wrong with the code!
The function doOCR takes a buffered image that is not possible while working in android! Bufferedimage uses IIOimage that is used in java but not in android!