2017-03-19 88 views
0

共享應用我已經創建了我的應用程序分享按鈕,它會通過意向分享我的整個應用程序。它是這樣工作的:重命名文件名通過意向

PackageManager pm = getPackageManager(); 
    ApplicationInfo ai = pm.getApplicationInfo(getPackageName(), 0); 
    File srcFile = new File(ai.publicSourceDir); 

    Intent share = new Intent(); 
    share.setAction(Intent.ACTION_SEND); 
    share.setType("application/vnd.android.package-archive"); 
    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(srcFile)); 
    startActivity(Intent.createChooser(share, "PersianCoders")); 
} catch (Exception e) { 
    Log.e("ShareApp", e.getMessage()); 
} 

現在,這個會送我的應用程序與我的包名。我需要分享喜歡這張例如前,它與我的應用程序名稱重命名:

PackageManager packageManager= getApplicationContext().getPackageManager(); 
String appName = (String) packageManager.getApplicationLabel(
packageManager.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA)); 
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0); 
String version = pInfo.versionName; 
String shareName = appName+"_"+"v"+version; 

有沒有辦法重命名我的包名這種「共享名」字符串?

回答

2

正如@CommonsWare提到的,你可以通過創建你的應用程序備份到的路徑去做,然後從該路徑共享:

public void shareApp(String package_name) { 
try { 
    PackageManager packageManager= getApplicationContext().getPackageManager(); 
    String appName = (String) packageManager.getApplicationLabel(
    packageManager.getApplicationInfo(getPackageName(), PackageManager.GET_META_DATA)); 
    PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0); 
    String version = pInfo.versionName; 
    String shareName = appName+"_"+"v"+version;    

    File f1; 
    File f2 = null; 
    final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null); 
    mainIntent.addCategory(Intent.CATEGORY_LAUNCHER); 
    final List<?> pkgAppsList = getPackageManager().queryIntentActivities(mainIntent, 0); 
    for (Object object : pkgAppsList) { 
    ResolveInfo info = (ResolveInfo) object; 
    if (info.activityInfo.packageName.equals(package_name)) { 
    f1 = new File(info.activityInfo.applicationInfo.publicSourceDir); 
    try { 
    f2 = new File(Environment.getExternalStorageDirectory().toString() + "/Apps"); 
    f2.mkdirs(); 
    f2 = new File(f2.getPath() + "/" + shareName + ".apk"); 
    f2.createNewFile(); 
    InputStream in = new FileInputStream(f1); 
    OutputStream out = new FileOutputStream(f2); 
    byte[] buf = new byte[4096]; 
    int len; 
    while ((len = in.read(buf)) > 0) { 
    out.write(buf, 0, len); 
    } 
    in.close(); 
    out.close(); 
    System.out.println("File copied."); 
    } catch (FileNotFoundException ex) { 
    System.out.println(ex.getMessage() + " in the specified directory."); 
    } catch (IOException e) { 
    System.out.println(e.getMessage());} 
    } 
    } 
    Intent share = new Intent(); 
    share.setAction(Intent.ACTION_SEND); 
    share.setType("application/vnd.android.package-archive"); 
    share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f2)); 
    startActivity(Intent.createChooser(share, "PersianCoders")); 
    } catch (Exception e) { 
    Log.e("ShareApp", e.getMessage()); 
    } 
} 

,並調用它你喜歡的地方:

shareApp(getPackageName()); 
0

有沒有辦法用這個「shareName」字符串重命名我的包名?

不直接。 ACTION_SEND中沒有「使用此替代文件名」選項。

您可以:

  • 使原始文件的副本,新的文件名,並分享該副本,或

  • 實現一個ContentProvider,一個返回文件的內容時,收到Uri與新的文件名

後者更加複雜,但也解決了您Uri.fromFile() PR問題:在Android 7.0+設備上運行時,如果將targetSdkVersion提升到24或更高,那麼這種方法將無法使用。