I am trying to create an application to scan two barcodes and then save the informations in a accessible text file on my phone. Can someone help?
MainActivity.java :
package com.example.barcodescanjava;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.activity.result.ActivityResultLauncher;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.journeyapps.barcodescanner.ScanContract;
import com.journeyapps.barcodescanner.ScanOptions;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
Button btn_scan1;
Button btn_scan2;
Button btn_store;
TextView barcode1;
TextView barcode2;
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn_store = findViewById(R.id.btn_store);
btn_scan1 = findViewById(R.id.btn_scan1);
btn_scan2 = findViewById(R.id.btn_scan2);
barcode1 = findViewById(R.id.barcode1);
barcode2 = findViewById(R.id.barcode2);
btn_scan1.setOnClickListener(v ->
{
i = 0;
scanCode();
});
btn_scan2.setOnClickListener(v ->
{
i = 1;
scanCode();
});
btn_store.setOnClickListener(v -> {
AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
builder2.setTitle("Occhio");
String textToSave = barcode1.getText().toString();
String textToSave2 = barcode2.getText().toString();
if(!textToSave.equals("Sorgente") && !textToSave2.equals("Destinazione")) {
String filename = "Inventario.txt";
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
if (!dir.exists()) {
dir.mkdirs(); // Crea la directory se non esiste
}
File file = new File(dir, filename);
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write((textToSave + " " + textToSave2).getBytes());
builder2.setMessage("SALVATO");
} catch (IOException e) {
e.printStackTrace();
builder2.setMessage("Errore durante il salvataggio del file");
}
builder2.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
barcode1.setText("Sorgente");
barcode2.setText("Destinazione");
} else {
builder2.setMessage("Devi scansionare entrambi i codici");
builder2.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
}
});btn_store.setOnClickListener(v -> {
AlertDialog.Builder builder2 = new AlertDialog.Builder(MainActivity.this);
builder2.setTitle("Occhio");
String textToSave = barcode1.getText().toString();
String textToSave2 = barcode2.getText().toString();
if(!textToSave.equals("Sorgente") && !textToSave2.equals("Destinazione")) {
String filename = "Inventario.txt";
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
if (!dir.exists()) {
dir.mkdirs(); // Crea la directory se non esiste
}
File file = new File(dir, filename);
try (FileOutputStream fos = new FileOutputStream(file)) {
fos.write((textToSave + " " + textToSave2).getBytes());
builder2.setMessage("SALVATO");
} catch (IOException e) {
e.printStackTrace();
builder2.setMessage("Errore durante il salvataggio del file");
}
builder2.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
barcode1.setText("Sorgente");
barcode2.setText("Destinazione");
} else {
builder2.setMessage("Devi scansionare entrambi i codici");
builder2.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
}
});
}
private void scanCode() {
ScanOptions options = new ScanOptions();
options.setPrompt("Volume su per la torcia");
options.setBeepEnabled(true);
options.setOrientationLocked(true);
options.setCaptureActivity(CaptureAct.class);
barLauncher.launch(options);
}
ActivityResultLauncher<ScanOptions> barLauncher = registerForActivityResult(new ScanContract(), result -> {
if (result.getContents() != null) {
if(i==0){
barcode1.setText(result.getContents());
}
else{
barcode2.setText(result.getContents());
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Result");
builder.setMessage(result.getContents());
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
}).show();
}
});
}
AndroidManifest.xml :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<uses-feature
android:name="android.hardware.camera"
android:required="false" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="32"
tools:ignore="ScopedStorage" />
<uses-permission android:name="MANAGE_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.BarcodeScanjava"
android:installLocation="preferExternal"
tools:targetApi="34">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity
android:name=".CaptureAct"
android:screenOrientation="fullSensor"
android:stateNotNeeded="true"
android:theme="@style/zxing_CaptureTheme"
tools:ignore="DiscouragedApi" />
</application>
</manifest>
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btn_scan1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="500dp"
android:text="Scansiona Sorgente" />
<Button
android:id="@+id/btn_scan2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="400dp"
android:text="Scansiona Destinazione" />
<Button
android:id="@+id/btn_store"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="600dp"
android:text="Salva" />
<TextView
android:id="@+id/barcode1"
android:layout_width="296dp"
android:layout_height="33dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
android:text="Sorgente" />
<TextView
android:id="@+id/barcode2"
android:layout_width="296dp"
android:layout_height="33dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Destinazione" />
</RelativeLayout>
I tried with different methods, but none of them worked.
I expect the file to be saved in the documents folder and then be accessible and readable by the user.
New contributor
Paolo Morello is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.