After zxing scans a barcode how do I get it to search for a barcode saved in a text file saved in the raw resources folder? Do these need to be separate activities? I feel like this is a simple task. I have the scanner working perfect I am hung up on how to compare scan result with txt file.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null) {
if (result.getContents() == null) {
Log.d("MainActivity", "cancelled");
Toast.makeText(this, "cancelled", Toast.LENGTH_LONG).show();
} else {
Log.d("MainActivity", "scanned");
Toast.makeText(this, "scanned: " + result.getContents(), Toast.LENGTH_LONG).show();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Cursor c = db.getWordMatches(query, null);
}
try {
FileInputStream in = new FileInputStream(R.raw.upc);
int len = 0;
byte[] data1 = new byte[1024];
while (-1 != (len = in.read(data1))) {
if (new String(data1, 0, len).contains(result.getContents()))
//do something...
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Related
I want to scan barcode that contains REST API, result.getContents will receive url . I have try to fill barcode with json format without using uriparse below and it's work. My problem is how to convert rest api to json.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
IntentResult result = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (result != null){
if (result.getContents() == null){
Toast.makeText(this, "Result not found", Toast.LENGTH_SHORT).show();
}else{
// if qrcode are fil
try{
String url = Uri.parse(result.getContents())
.buildUpon()
.build()
.toString();
// converting the data json
JSONObject object = new JSONObject(url);
// setting value to textview
textViewName.setText(object.getString("ceramics_name"));
textViewDisplay.setText(object.getString("ceramics_category"));
textViewStorage.setText(object.getString("storage_stock"));
Toast.makeText(this, result.getContents(), Toast.LENGTH_SHORT).show();
}catch (JSONException e){
e.printStackTrace();
Toast.makeText(this, result.getContents(), Toast.LENGTH_SHORT).show();
}
}
}else{
super.onActivityResult(requestCode, resultCode, data);
}
}
I am creating a App, which should save reference documents, saved on the device.
I am receiving the content URI for these documents in the following way:
private void selectDocument() {
Intent chooseFile;
chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
chooseFile.setType("*/*");
if (chooseFile.resolveActivity(getPackageManager()) != null)
startActivityForResult(chooseFile, ACTIVITY_CHOOSE_FILE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
if (resultCode != RESULT_OK) return;
if (requestCode == ACTIVITY_CHOOSE_FILE) {
try {
binding.txtLink.setText(URLDecoder.decode(data.getDataString(),"UTF-8"));
binding.btnLink.setTag(false);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
after displaying the url to the user, he should be able to open the document.
And i am not really sure how to do that, since I only get the contentURI.
What i am currently trying is this:
private void openDocument() {
String file = binding.txtLink.getText().toString();
if (file.isEmpty()) {
return;
}
Intent intent;
String extension = file.substring(file.lastIndexOf(".") + 1);
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(file));
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
} else {
Snackbar.make(binding.root, "Keine Anwendung zum öffnen der Datei installiert", Snackbar.LENGTH_LONG).show();
}
}
This already opens the correct App, but they can't find the files.
What would i need to do, to enable this behavior?
Or do i have to copy the files to my app directory and open them from there?
I am using this two functions to open the image gallery and to choose a picture. I have the uri of the picture and the picture itself in "inputStream".
My question is how can I move the picture or to copy it in the folder drawable from android studio ?
public void onImageGalleryClicked(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
File pictureDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String pictureDirectoryPath = pictureDirectory.getPath();
Uri data = Uri.parse(pictureDirectoryPath);
photoPickerIntent.setDataAndType(data, "image/*");
startActivityForResult(photoPickerIntent, IMAGE_GALLERY_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == IMAGE_GALLERY_REQUEST)
Uri imageUri = data.getData();
InputStream inputStream;
try {
inputStream = getContentResolver().openInputStream(imageUri);
} catch (FileNotFoundException e) {
e.printStackTrace()
Toast.makeText(this, "Unable to open image", Toast.LENGTH_LONG).show();
}
}
}
}
I would have done it like below:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (resultCode == RESULT_OK) {
if (requestCode == IMAGE_GALLERY_REQUEST) {
Uri imageUri = data.getData();
Long tsLong = System.currentTimeMillis();
String now = tsLong.toString();
String destitaion =folderPath+"/"+now+".jpg";
savefile(imageUri.getPath(),destitaion);
}
}
}
void savefile(String sourceFilename,String destinationFilename)
{
//String sourceFilename= sourceuri.getPath();
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
boolean exp_occuered=false;
try {
bis = new BufferedInputStream(new FileInputStream(sourceFilename));
bos = new BufferedOutputStream(new FileOutputStream(destinationFilename, false));
byte[] buf = new byte[1024];
bis.read(buf);
do {
bos.write(buf);
} while(bis.read(buf) != -1);
} catch (IOException e) {
e.printStackTrace();
exp_occuered=true;
}
finally {
try {
if (bis != null) bis.close();
if (bos != null) bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(!exp_occuered)
{
//the image has been saved properly
}
}
I am trying to add in my App a function to use the camera to store photos in my device.
At the beginning i use the camera like this:
mButtonCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
count++;
String file = dir+prova+".jpg";
File newFile = new File(file);
try {
newFile.createNewFile();
}catch (IOException e){}
Uri outputFileUri = Uri.fromFile(newFile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,outputFileUri);
Log.v("CameraDemo", "Pic savedNO");
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
});
And then for the OnActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.v("CameraDemo", "Pic saved");
Log.v("Salva Foto", dir+prova+".jpg");
myDBHandler.addPhoto(prova,dir+prova+".jpg");
}
}
Until here evrything is ok but when I try to retrieve the photo using this:
InputStream URLcontent = null;
try {
URLcontent = (InputStream) new URL(fotoSI).getContent();
} catch (IOException e) {
e.printStackTrace();
}
Drawable image = Drawable.createFromStream(URLcontent, fotoSI);
mImageCamera.setImageDrawable(image);
It doesn´t return any photo that is what the Log says me:
java.net.MalformedURLException: Protocol not found: /storage/emulated/0/Pictures/
I am trying everything but without results.
logcat message remind:
failure delivering result ResultInfo{who = null,request=131073,result=-1,date=null}
fatal exception at com.example.newpingziyi.FindFragment.doPhoto(FindFragment.java:178);
178lines:
Cursor cursor = getActivity().managedQuery(photoUri, pojo, null, null,
null);
onActivityResult after photos pick up
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
doPhoto(requestCode, data);
}
super.onActivityResult(requestCode, resultCode, data);
}
private void doPhoto(int requestCode, Intent data) {
if (requestCode == SELECT_PIC_BY_PICK_PHOTO) {
if (data == null) {
Toast.makeText(getActivity(), R.string.photo_err,
Toast.LENGTH_LONG).show();
return;
}
photoUri = data.getData();
if (photoUri == null) {
Toast.makeText(getActivity(), R.string.photo_err,
Toast.LENGTH_LONG).show();
return;
}
}
String[] pojo = { MediaColumns.DATA };
// fatal exception
Cursor cursor = getActivity().managedQuery(photoUri, pojo, null, null,
null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
try {
if (Integer.parseInt(Build.VERSION.SDK) < 14) {
cursor.close();
}
} catch (Exception e) {
Log.e(TAG, "error:" + e);
}
}
Log.i(TAG, "imagePath = " + picPath);
if (picPath != null) {
Intent startEx = new Intent(getActivity(), PhotoPre.class);
startEx.putExtra(SAVED_IMAGE_DIR_PATH, picPath);
startActivity(startEx);
} else {
Toast.makeText(getActivity(), R.string.photo_err, Toast.LENGTH_LONG)
.show();
}
}
use this
cursor.moveToLast();
instead of cursor.moveToFirst();