0


When I wrote code in Android Studio, couldn't use getAssets without MainActivity.
Here is MainActivity.

package com.example.maguro.mnist_bg2;

import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.content.res.AssetManager;
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;

public class MainActivity extends AppCompatActivity {
    CanvasView canvasview; //already exist CanvasView class
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        canvasview = new CanvasView(this);
        setContentView(canvasview);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()) {
            case R.id.item1:
                canvasview.onReset();
                break;
            case R.id.item2:
                canvasview.onDetect();
                break;
            case R.id.item3:
                finish();
                break;
        }
        return true;
    }

}

And here is another example class.

package com.example.maguro.mnist_bg2;

import android.content.Context;
import android.content.res.AssetManager;
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;


public class DigitDetector {
    static{
        System.loadLibrary("tensorflow_inference");
    }

    protected void onDetect(byte[] bytes){
        TensorFlowInferenceInterface inference = new TensorFlowInferenceInterface(getAssets(), "beginner-graph.pb");
    }

}

Error point is almost last line TensorFlowInferenceInterface inference = new TensorFlowInferenceInterface(getAssets(), "beginner-graph.pb"); Although Android Studio said

Cannot resolve method 'getAssets()

Android Studio didn't say that error in MainActivity

Even if I equate another class module with MainActivity, Android Studio said same error.

Why did error occur?

Please lend me your hands.

Sergei Bubenshchikov
  • 5,275
  • 3
  • 33
  • 60
maguro
  • 84
  • 3
  • 10

2 Answers2

3
import android.content.Context;
import android.content.res.AssetManager;
import org.tensorflow.contrib.android.TensorFlowInferenceInterface;


public class DigitDetector {

Context context;

public DigitDetector(Context context) {
    this.context =context;
}

static{
    System.loadLibrary("tensorflow_inference");
}

protected void onDetect(byte[] bytes){
    TensorFlowInferenceInterface inference = new TensorFlowInferenceInterface(context.getAssets(), "beginner-graph.pb");
}

}

Try to create constructor and pass activity context on this. and use it for 'getAssets()' method.

Payal Sorathiya
  • 756
  • 1
  • 8
  • 23
1

Create a Constructor in your class. eg:-

Context context;
DigitDetector(Context ctx){
context = ctx;
}

and use it as.

context.getAssets()....
SRB Bans
  • 3,096
  • 1
  • 10
  • 21