我正在尝试使用excel库从assets文件夹中打开一个excel文件但是应用程序崩溃我设法将两个代码相互组合,这就是为什么它无法正常工作。 有帮助吗? 谢谢。
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);
}
});
错误
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)
答案 0 :(得分:0)
new File("file:///android_asset/data.xlsx")
无效。资产不是文件;它们只是ZIP档案中的条目,即您的APK文件。
如果您的图书馆接受InputStream
作为加载电子表格的来源,请使用getResources().getAssets().open("data.xlsx")
在您的电子表格中获得此类InputStream
。否则,您需要将该文件从资产复制到内部存储,以便您可以将实际File
提供给库。
答案 1 :(得分:0)
您可以尝试这样的内容,例如
File file = new File(m.getInstrumentation().getContext().getExternalFilesDir(null), filename);
FileInputStream fis = new FileInputStream(file);
OR
您可以尝试使用此代码。如
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;
}
}
}
如果您对上述代码有任何疑问。请关注此How to read files (Images or text files) from Assets folder?