如您所知,文件具有扩展名和mime类型。
但这两个属性不够智能,两个确定文件的确切类型。例如,我有一个package com.example.administrator.mosbeau;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.provider.Settings;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
@SuppressWarnings("deprecation")
public class IndexActivity extends Activity {
Button joinbutton, signbutton;
UserLocalStore userLocalStore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
ConnectivityManager cm = (ConnectivityManager)this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null &&
activeNetwork.isConnectedOrConnecting();
//boolean isWiFi = activeNetwork.getType() == ConnectivityManager.TYPE_WIFI;
if(isConnected){
}else{
nointernet();
}
userLocalStore = new UserLocalStore(this);
// Locate the button in activity_main.xml
joinbutton = (Button) findViewById(R.id.joinbutton);
signbutton = (Button) findViewById(R.id.signbutton);
// Capture button clicks
joinbutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(IndexActivity.this, RegisterActivity.class);
startActivity(myIntent);
}
});
signbutton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(IndexActivity.this, LoginActivity.class);
startActivity(myIntent);
}
});
}
public void onStart() {
super.onStart();
if(authenticate() == true){
/*Intent myIntent = new Intent(IndexActivity.this, MainActivity.class);
startActivity(myIntent);*/
displayUserDetails();
}
}
private boolean authenticate() {
if (userLocalStore.getLoggedInUser() == null) {
return false;
}
return true;
}
private void displayUserDetails(){
User user = userLocalStore.getLoggedInUser();
if(user.customers_id==""){
}else{
Intent myIntent = new Intent(IndexActivity.this, MainActivity.class);
startActivity(myIntent);
}
}
public void nointernet(){
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
dialogBuilder.setMessage("There seems to be a problem with your connection.");
dialogBuilder.setNegativeButton("Edit Settings", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Stop the activity
startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
}
});
dialogBuilder.setPositiveButton("Reload", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Stop the activity
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
AlertDialog dialog = dialogBuilder.show();
TextView messageText = (TextView)dialog.findViewById(android.R.id.message);
messageText.setGravity(Gravity.CENTER);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
dialog.show();
}
}
文件,我将其重命名为 public void update(float delta)
。因此,如果我尝试以编程方式找到它的类型或mime类型,结果将是.exe
或.png
但我想读取文件的标题并找出确切的文件类型。是否有可能以编程方式使用C#语言?
修改
当然有些问题与我的相似,但是你知道文件以十六进制字符串开头,例如:
images/png
.png
等等。
文件类型是否具有类似的起始十六进制格式?
答案 0 :(得分:1)
您可以尝试检查文件中的某些文件签名或幻数。这是known file signatures列表的链接,似乎是最新的:
还有另一种方法可以做到这一点。使用Winista MIME Detector。
有一个XML文件mime-type.xml
,其中包含有关文件类型和用于标识内容类型的签名的信息。您将需要此文件来创建MimeTypes
对象的实例。创建MimeTypes
对象后,请调用GetMimeType
方法获取流的MimeType
。如果无法确定mime类型,则从此方法返回null对象。以下代码段演示了如何使用库。
示例:
MimeTypes g_MimeTypes = new MimeTypes("mime-types.xml");
sbyte [] fileData = null;
using (System.IO.FileStream srcFile =
new System.IO.FileStream(strFile, System.IO.FileMode.Open))
{
byte [] data = new byte[srcFile.Length];
srcFile.Read(data, 0, (Int32)srcFile.Length);
fileData = Winista.Mime.SupportUtil.ToSByteArray(data);
}
MimeType oMimeType = g_MimeTypes.GetMimeType(fileData);