• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Object Recognition

Hi All,

I'm seeking some help for a project. I have 3 various activities (Main, Camera & Share). Main is simply a welcome page, Camera I guess speaks for itself however with an option button to either select from the Gallery or to Take a Photo (and identify what it is) and a Share page, to share the image and info to facebook.

If I run each of the activities separately, all is OK (via Edit Config in Studio). The Main loads, followed by the Camera activity however this is where I run into my first problems.

a. Firstly, the menu button allows the user to select 'Take Photo' but then the app crashes. I believe I have the correct permissions in the Manifest?

b. Selecting the Choose from Gallery, the gallery does appear and allows for the selection of an image, but when it returns to the previous page it doesn't load in the imageview?

c. Finally, I wish to add the Google Cloud Vision API, where the app can take a photo and then display info about what the object is. This is the tedious part. I've googled for weeks, trial and error, but to no avail. I have registered for an API however.

There is this link https://github.com/GoogleCloudPlatform/cloud-vision/tree/master/android where I have attempted to follow the readme guide, but haven't had much luck.

Instead of a database within the app, I thought the Google Cloud Vision would be a great idea?

I have been developing in Android Studio.

This is my first attempt at an app, btw.

I look forward to your replies.

Nick.
 

Attachments

To answer issue a) we need to see the stack trace from the Logcat view.

Hello LV426,

Thanks for your quick reply.

I think I have worked out how to retrieve the 'Error' entries from the Logcat. I'm not entirely sure what I am looking for here.

It is quite lengthy -

01-02 10:09:00.954 1503-4452/? E/AudioFlinger: not enough memory for AudioTrack size=131296
01-02 10:09:00.954 1503-4452/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
01-02 10:09:00.954 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord: AudioFlinger could not create record track, status: -12
01-02 10:09:00.956 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
01-02 10:09:00.956 2905-2957/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
01-02 10:09:05.990 1503-4452/? E/AudioFlinger: not enough memory for AudioTrack size=131296
01-02 10:09:05.990 1503-4452/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
01-02 10:09:05.990 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord: AudioFlinger could not create record track, status: -12
01-02 10:09:05.991 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
01-02 10:09:05.992 2905-2957/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
01-02 10:09:11.050 1503-4345/? E/AudioFlinger: not enough memory for AudioTrack size=131296
01-02 10:09:11.050 1503-4345/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
01-02 10:09:11.050 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord: AudioFlinger could not create record track, status: -12
01-02 10:09:11.051 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
01-02 10:09:11.051 2905-2957/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
01-02 10:09:16.105 1503-4452/? E/AudioFlinger: not enough memory for AudioTrack size=131296
01-02 10:09:16.105 1503-4452/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
01-02 10:09:16.106 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord: AudioFlinger could not create record track, status: -12
01-02 10:09:16.107 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
01-02 10:09:16.107 2905-2957/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
01-02 10:09:21.135 1503-4452/? E/AudioFlinger: not enough memory for AudioTrack size=131296
01-02 10:09:21.135 1503-4452/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
01-02 10:09:21.135 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord: AudioFlinger could not create record track, status: -12
01-02 10:09:21.137 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
01-02 10:09:21.137 2905-2957/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
01-02 10:09:26.175 1503-4452/? E/AudioFlinger: not enough memory for AudioTrack size=131296
01-02 10:09:26.175 1503-4452/? E/AudioFlinger: createRecordTrack_l() initCheck failed -12; no control block?
01-02 10:09:26.175 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord: AudioFlinger could not create record track, status: -12
01-02 10:09:26.182 2905-2957/com.google.android.googlequicksearchbox:search E/AudioRecord-JNI: Error creating AudioRecord instance: initialization check failed with status -12.
01-02 10:09:26.182 2905-2957/com.google.android.googlequicksearchbox:search E/android.media.AudioRecord: Error code -20 when initializing native AudioRecord object.
 
Got it!

So I think it's the at com.example.nb.crunchnpeel.CameraActivity$2.onClick(CameraActivity.java:64) line I should be concerned about?

01-03 16:49:17.262 8450-8450/com.example.nb.crunchnpeel D/ViewRootImpl@c112ba8[CameraActivity]: ViewPostImeInputStage processPointer 0
01-03 16:49:17.362 8450-8450/com.example.nb.crunchnpeel D/ViewRootImpl@c112ba8[CameraActivity]: ViewPostImeInputStage processPointer 1
01-03 16:49:17.363 8450-8450/com.example.nb.crunchnpeel D/AbsListView: onTouchUp() mTouchMode : 0
01-03 16:49:17.601 8450-8450/com.example.nb.crunchnpeel D/AndroidRuntime: Shutting down VM
01-03 16:49:17.604 8450-8450/com.example.nb.crunchnpeel E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.nb.crunchnpeel, PID: 8450
android.os.FileUriExposedException: file:///storage/emulated/0/temp.jpg exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1799)
at android.net.Uri.checkFileUriExposed(Uri.java:2346)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:832)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9530)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9515)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1525)
at android.app.Activity.startActivityForResult(Activity.java:4403)
at android.app.Activity.startActivityForResult(Activity.java:4362)
at com.example.nb.crunchnpeel.CameraActivity$2.onClick(CameraActivity.java:64)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1259)
at android.widget.AdapterView.performItemClick(AdapterView.java:339)
at android.widget.AbsListView.performItemClick(AbsListView.java:1718)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:4184)
at android.widget.AbsListView$13.run(AbsListView.java:6754)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6776)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)


The CameraActivity.java currently reads as -

package com.example.nb.crunchnpeel;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io_OutputStream;

public class CameraActivity extends Activity {

ImageView viewImage;
Button b;

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
b=(Button)findViewById(R.id.btnSelectPhoto);
viewImage=(ImageView)findViewById(R.id.viewImage);
b.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View v) {
selectImage();
}
});
}

@override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

private void selectImage() {

final CharSequence[] options = { "Take Photo", "From Gallery","Cancel" };
AlertDialog.Builder builder = new AlertDialog.Builder(CameraActivity.this);
builder.setTitle("Add Photo");
builder.setItems(options, new DialogInterface.OnClickListener() {

@override
public void onClick(DialogInterface dialog, int item) {
if (options[item].equals("Take Photo"))
{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 1);
}
else if (options[item].equals("From Gallery"))
{
Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
}
else if (options[item].equals("Cancel")) {
dialog.dismiss();
}
}
});
builder.show();
}
@override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
File f = new File(Environment.getExternalStorageDirectory().toString());
for (File temp : f.listFiles()) {
if (temp.getName().equals("temp.jpg")) {
f = temp;
break;
}
}
try {
Bitmap bitmap;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeFile(f.getAbsolutePath(),
bitmapOptions);

viewImage.setImageBitmap(bitmap);

String path = android.os.Environment
.getExternalStorageDirectory()
+ File.separator
+ "Phoenix" + File.separator + "default";
f.delete();
OutputStream outFile = null;
File file = new File(path, String.valueOf(System.currentTimeMillis()) + ".jpg");
try {
outFile = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, outFile);
outFile.flush();
outFile.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
} else if (requestCode == 2) {

Uri selectedImage = data.getData();
String[] filePath = { MediaStore.Images.Media.DATA };
Cursor c = getContentResolver().query(selectedImage,filePath, null, null, null);
c.moveToFirst();
int columnIndex = c.getColumnIndex(filePath[0]);
String picturePath = c.getString(columnIndex);
c.close();
Bitmap thumbnail = (BitmapFactory.decodeFile(picturePath));
Log.w("image from gallery.***.", picturePath+"");
viewImage.setImageBitmap(thumbnail);
}
}
}
}

Thoughts?
 
Back
Top Bottom