尝试从Android上的Cache访问文件时出现NullPointerException

时间:2018-05-30 16:46:48

标签: java android nullpointerexception temporary-files

我在尝试访问我在同一个应用程序中的Cache中创建的文件时遇到Null指针异常。以下是我的代码的基本流程:

public class MyActivity extends AppCompatActivity {
private static final String MP4_EXT = ".mp4";
public static String PACKAGE_NAME ;
Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
  PACKAGE_NAME = getApplicationContext().getPackageName();
  context = this;
  super.onCreate(savedInstanceState);
  new MyTask(getFileFromIntent(getIntent()), context).execute();
}

private static String stripExtension(String inputName) {
  // some implementation
}

private class MyTask extends AsyncTask<Void, Void, File> {
  private final String inputFilename;
  private Context mContext;
  private static final String TAG = "MyTask";

  public MyTask(String filename, Context context) {
    this.inputFilename = filename;
    this.mContext = context;
  }

@Override
protected File doInBackground(Void... params) {
  if (inputFilename == null) {
    return null;
  }

  File inputFile = new File(inputFilename);
   byte[] inputBytes;
   try {
      inputBytes = Files.toByteArray(inputFile);
   } catch (Exception e) {
     return null;
   }
   String outputName = stripExtension(inputFile.getName());
   File outputFile = File.createTempFile(outputName, MP4_EXT, mContext.getCacheDir());
   // code to fill up outputFile
  MediaStoreUtil.updateFile(MyActivity.this, outputFile);
  return outputFile;
}
  @Override
  protected void onPostExecute(File outputFile) {
   if (outputFile == null) {
   finish();
   return;
  }
  Uri uri;
  try {
    uri  = FileProvider.getUriForFile(context, PACKAGE_NAME, outputFile);
   } catch (Exception e){
    Log.e(TAG, "Caught exception while trying to get temp file: ", e);
    return;
   }

   // code to use the video file

  };
 }
}

我尝试使用uri尝试获取FileProvider的行中的代码失败。这是一个例外:

outputFile /data/user/0/com.example.package/cache/YI3D0496495423980834512065.mp4
Caught exception while trying to get temp file: 
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.PackageItemInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:605)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:579)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:417)
at com.example.package.MyActivity$MyTask.onPostExecute(MyActivity.java:150)
at com.example.package.MyActivity$MyTask.onPostExecute(MyActivity.java:80)
at android.os.AsyncTask.finish(AsyncTask.java:695)
at android.os.AsyncTask.-wrap1(Unknown Source:0)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:712)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

我的清单文件中有以下代码:

<application>
 <provider>
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.package"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
           android:name="android.support.FILE_PROVIDER_PATHS"
           android:resource="@xml/file_paths" />
  </provider>
 </application>

我已向res/xml/file_paths.xml添加了一个新文件,其中包含:

 <?xml version="1.0" encoding="utf-8"?>
 <paths xmlns:android="http://schemas.android.com/apk/res/android"> 
  <cache-path name="videos" path="" />
 </paths>

0 个答案:

没有答案