Mostafa Addam
Mostafa Addam

Reputation: 7296

how to open an excel file from assets folder

I am trying to open an excel file from the assets folder using the excel library but the application crashes i managed to combine two codes with each other thats why its not working. Any help? Thank you.

             public class MainActivity extends Activity {
              Button open;
        @Override
         protected void onCreate(Bundle savedInstanceState) 
        {
         super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        open=(Button)findViewById(R.id.button1);



          open.setOnClickListener(new View.OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            // TODO Auto-generated method stub
            File file = new File("file:///android_asset/data.xlsx");
            try {
                Workbook workbook = Workbook.getWorkbook(file);
            } catch (BiffException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 
            MimeTypeMap map = MimeTypeMap.getSingleton();
            String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
            String type = map.getMimeTypeFromExtension(ext);

            if (type == null)
               type = "*/*";

            Intent intent = new Intent(Intent.ACTION_VIEW);
            Uri data = Uri.fromFile(file);

            intent.setDataAndType(data, type);

            startActivity(intent);
        }
    });

error

      04-30 15:22:54.642: E/AndroidRuntime(22148): FATAL EXCEPTION: main
      04-30 15:22:54.642: E/AndroidRuntime(22148): Process: com.ecxel, PID: 22148 
      04-30 15:22:54.642: E/AndroidRuntime(22148): java.lang.VerifyError: com/ecxel/MainActivity$1
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at  com.ecxel.MainActivity.onCreate(MainActivity.java:29)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at android.app.Activity.performCreate(Activity.java:5312)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at android.app.ActivityThread.access$800(ActivityThread.java:156)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at android.os.Handler.dispatchMessage(Handler.java:102)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at android.os.Looper.loop(Looper.java:157)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at android.app.ActivityThread.main(ActivityThread.java:5872)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at java.lang.reflect.Method.invokeNative(Native Method)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at java.lang.reflect.Method.invoke(Method.java:515)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
      04-30 15:22:54.642: E/AndroidRuntime(22148):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)

Upvotes: 2

Views: 3428

Answers (2)

kablu
kablu

Reputation: 659

You can try like this.such as

    File file = new File(m.getInstrumentation().getContext().getExternalFilesDir(null), filename);
 FileInputStream fis = new FileInputStream(file);

OR

You can try like this codes. Such as

public class ReadFileAssetsActivity extends Activity {

    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView txtContent = (TextView) findViewById(R.id.txtContent);
        TextView txtFileName = (TextView) findViewById(R.id.txtFileName);
        ImageView imgAssets = (ImageView) findViewById(R.id.imgAssets);

        AssetManager assetManager = getAssets();

        // To get names of all files inside the "Files" folder
        try {
            String[] files = assetManager.list("Files");

            for(int i=0; i<files.length; i++)            {               txtFileName.append("\n File :"+i+" Name => "+files[i]);
            }
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }

        // To load text file
        InputStream input;
        try {
            input = assetManager.open("helloworld.txt");

             int size = input.available();
             byte[] buffer = new byte[size];
             input.read(buffer);
             input.close();

             // byte buffer into a string
             String text = new String(buffer);

             txtContent.setText(text);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // To load image
        try {
            // get input stream
            InputStream ims = assetManager.open("android_logo_small.jpg");

            // create drawable from stream
            Drawable d = Drawable.createFromStream(ims, null);

            // set the drawable to imageview
            imgAssets.setImageDrawable(d);
        }
        catch(IOException ex) {
            return;
        }
    }
}

If your any query about above codes. please follow this How to read files (Images or text files) from Assets folder?

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006674

new File("file:///android_asset/data.xlsx") will not work. Assets are not files; they are merely entries in the ZIP archive that is your APK file.

If your library accepts an InputStream as a source for loading the spreadsheet, use getResources().getAssets().open("data.xlsx") to get such an InputStream on your spreadsheet. Otherwise, you will need to copy that file from assets to internal storage, so that you can have an actual File to give to the library.

Upvotes: 1

Related Questions