2016-08-01 56 views
0

我在Android的自定义类,很简单,我打电话给在我的应用程序文件,它来自一个特定的URLAndroid的 - 自定义类进口不同不知道为什么

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Environment; 

import java.io.File; 

public class DownloadAndShare 
{ 
    Activity activity; 
    Context context; 
    ProgressDialog dialog; 
    String URL; 

    public DownloadAndShare(Activity activity, Context context, String URL) { 
     this.activity = activity; 
     this.context = context; 
     this.URL = URL; 
    } 

    public void startSharing() 
    { 
     //Start shareIntent for this file 
     File fileWithinMyDir = Environment.getExternalStorageDirectory(); 
     String[] fn = URL.split("/"); 
     String fileName = fn[fn.length-1]; 
     File storageFolder = new File(fileWithinMyDir.toString() + "/myappname"); 
     //If storage folder does not yet exists, create it 
     if(!storageFolder.exists()){ 
      storageFolder.mkdirs(); 
     } 
     final String PATH = fileWithinMyDir.toString() + "/myappname/" + fileName; 
     //Check if file exists in internal storage 
     File file = new File(PATH.replace(" ", "")); 
     if (!file.exists()){ 
      //file does not exists, download to internal storage for use 
      new executeDownloadAndShare().execute(PATH.replace(" ", ""), URL); 
     } else { 
      Intent shareIntent = Globals.getShareIntent(PATH.replace(" ", ""), false); 
      activity.startActivity(Intent.createChooser(shareIntent, "Share using...")); 
     } 
    } 

    private class executeDownloadAndShare extends AsyncTask<String, Integer, Integer> { 

     String filePath; 

     @Override 
     protected void onPreExecute() { 
      dialog = ProgressDialog.show(context, "", context.getResources().getString(R.string.general_msg_please_wait)); 
     } 

     @Override 
     protected Integer doInBackground(String... params) { 
      filePath = params[0]; 
      try { 
       Globals.DownloadFromUrl(params[0], params[1]); 
      } catch (Exception e) { 
       return AppConstants.STATUS_FAIL; 
      } 
      return AppConstants.STATUS_SUCCESS; 
     } 

     @Override 
     protected void onPostExecute(Integer result) { 
      dialog.dismiss(); 
      Intent shareIntent = Globals.getShareIntent(filePath, false); 
      activity.startActivity(Intent.createChooser(shareIntent, "Share using...")); 
     } 
    } 
} 

下载和启动份额意图出于某种原因,当我尝试在我的一个文件中创建它的实例时,它需要有完整的文件com.myappname.android.MyCustomClass myCustomClass = new com.myappname.android.MyCustomClass();为了使文件只有MyCustomClass myCustomClass = new MyCustomClass(),并且它工作正常,但是在一些文件中它希望整个路径。

有没有人有任何见识,为什么会出现这种情况?我称之为的这两个文件都是扩展Fragment,其中一个工作,另一个需要完整路径。我想也许这是一种私人方法,它应该是公开的,但我找不到任何错误。

+0

您是否尝试过在重新启动之前清理项目并从设备/模拟器中卸载应用程序? – parohy

+0

@parohy - 我做了一个干净的项目,是的,但这似乎没有帮助。我会再试一次。 – Phil

+0

@parohy - 我清理了项目并重新启动了Android Studio,它工作正常。去搞清楚。谢谢!!! – Phil

回答

1

清理你的项目并从设备/模拟器卸载应用程序,然后再次启动它。如果没有帮助,请重新启动Android Studio。

相关问题