2017-03-19 17 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、1を実装する新しいファイル名の下に、元のファイルのコピーを作成し、共有コピーすること、または

  • 新しいファイル名とUri

を受け、後者はより複雑であるだけでなく、あなたのUri.fromFile()広報を解決oblem:Android 7.0以降のデバイスでは、targetSdkVersionを24以上に上げると動作しません。

関連する問題