2017-07-01 17 views
1

アプリケーションから共有それAPKファイル名:共有変更私は次のコードで私のアプリを共有

ApplicationInfo app = getApplicationContext().getApplicationInfo(); 
String filePath = app.sourceDir; 
Intent intent = new Intent(Intent.ACTION_SEND); 
intent.setType("*/*"); 
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath))); 
startActivity(Intent.createChooser(intent, "Share app via")); 

しかし、ファイル名が「base.apk」は、どのようにそれを変更するには?

+0

なぜあなたは自分のプレイストアのリンクを共有しませんか?単に好奇心を求める。 –

+0

大学のプロジェクトで、店舗で公開されないため – Mahdi

答えて

0

APK名変更でアプリを共有するために、あなたは以下の方法を使用してSDカードにAPKファイルをコピーすることができ、APK今

public static File copyFile(ApplicationInfo appInfo, File folderName, String apkName) { 

    File initialFile = new File(appInfo.getSourceDir()); 
    File finalFile = new File(folderName, apkName); 

    try { 
     FileInputStream inStream = new FileInputStream(initialFile); 
     FileOutputStream outStream = new FileOutputStream(finalFile); 
     FileChannel inChannel = inStream.getChannel(); 
     FileChannel outChannel = outStream.getChannel(); 
     inChannel.transferTo(0, inChannel.size(), outChannel); 
     inStream.close(); 
     outStream.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    return finalFile; 
} 

作成のためApplicationItem、期待されるフォルダと新しい予想される名前を渡しますあなたがコピーしたファイルを使用する意図。最後に

public static Intent getShareIntent(File file) { 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_SEND); 
    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file)); 
    intent.setType("application/vnd.android.package-archive"); 
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    return intent; 
} 

アプリを共有することができ、

ApplicationInfo app = getApplicationContext().getApplicationInfo(); 
File newFile = copyFile(app, Environment.getExternalStorageDirectory(), "[Expected name].apk"); 

Intent shareIntent = getShareIntent(newFile); 
startActivity(Intent.createChooser(shareIntent, "Sharing [Appname] Apk")); 
+0

ApplicationItemクラスとAppUtilクラスが見つかりません。なぜですか?あなたの意見では、どこに問題がありますか? – Mahdi

+0

申し訳ありません私は答えを更新しました、上記のコピーとメソッドを共有することができます、それは私のアプリケーションのクラス名です。 –

+0

ありがとうございますが、copyFile()を呼び出すと、キャッチブロック – Mahdi

関連する問題