2016-08-01 54 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。

相關問題