J'utilise ce code pour envoyer le fichier APK de l'application sur un autre périphérique. Cela fonctionne sur Android 2.3.3, mais ne fonctionne pas sur Android 4 +.
Où est le problème? P>
J'ai enregistré le getpackagecodepath () code> et il renvoie le Fichier APK sur Android 4+, mais tout le code ne fonctionne pas, et lorsque Bluetooth démarre, il n'enverse rien. P>
ArrayList<Uri> uris = new ArrayList<Uri>();
Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
sendIntent.setType("application/vnd.android.package-archive");
uris.add(Uri.parse(getApplication().getPackageCodePath()));
sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
startActivity(Intent.createChooser(sendIntent, null));
6 Réponses :
Modifier la ligne: à p> et il devrait fonctionner dans tous les périphériques 4.x. Sinon, vous pouvez également faire quelque chose comme ceci: p> Les deux des manières ci-dessus fonctionnent pour moi. P> p>
Vous pouvez utiliser vérifier ici pour un exemple:
Transfert de fichier Bluetooth sur Android (même des types restreints) p> gettaplicationinfo (). Publicsourcedir code> au lieu de
gettaplication (). getpackagecodepath () code> Pour obtenir le chemin d'accès à l'APK de votre application, puis utilisez-le dans un
Action_send code>
Intention code> Pour envoyer le fichier via Bluetooth. P>
J'utilise ci-dessous le code pour envoyer APK. et ça marche
+1 J'ai essayé ce code, cela fonctionne dans les appareils Samsung mais ne travaille pas sur Moto G2. S'il vous plaît laissez-moi savoir comment trier le problème
InputStream in = null; OutputStream out = null; File apkPublicFilePath = new File( Environment .getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "myapk.apk"); try { in = getResources().openRawResource(R.raw.myapk); out = new FileOutputStream(apkPublicFilePath); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } // Close the streams out.flush(); out.close(); in.close(); } catch (IOException e) { Log.d(TAG, "Error in copy files"); } Intent sendIntent = new Intent(); sendIntent.setAction(Intent.ACTION_SEND); sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(apkPublicFilePath.getAbsoluteFile())); try { sendIntent.setType("application/vnd.android.package-archive"); startActivity(Intent.createChooser( sendIntent, PersianReshape.reshape(getResources().getString( R.string.msg_send)))); } catch (Exception e) { sendIntent.setType("*/*"); startActivity(Intent.createChooser( sendIntent, PersianReshape.reshape(getResources().getString( R.string.msg_send)))); }
J'espère que cela fonctionne:
// Get current ApplicationInfo to find .apk path ApplicationInfo app = getApplicationContext().getApplicationInfo(); String filePath = app.sourceDir; Intent intent = new Intent(Intent.ACTION_SEND); // MIME of .apk is "application/vnd.android.package-archive". // but Bluetooth does not accept this. Let's use "*/*" instead. intent.setType("*/*"); // Only use Bluetooth to send .apk intent.setPackage("com.android.bluetooth"); // Append file and send Intent intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(filePath))); startActivity(Intent.createChooser(intent, "Share app"));
ArrayList<Uri> uris = new ArrayList<Uri>(); Intent sendIntent = new Intent(Intent.ACTION_SEND_MULTIPLE); sendIntent.setType("*/*"); uris.add(Uri.fromFile(new File(getApplicationInfo().publicSourceDir))); sendIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); startActivity(Intent.createChooser(sendIntent, null)); Use / this worked for me
Êtes-vous sûr que votre appareil Android 4.0 est équipé pour gérer les transferts Bluetooth? Avez-vous essayé d'envoyer un fichier sur Bluetooth en utilisant un gestionnaire de fichiers OI ou quelque chose?
Oui, et je peux envoyer un fichier via mon téléphone avec les gestionnaires de fichiers
@Ata Avez-vous eu une réponse?
Nope, il semble que l'opération Android 4 bloquée sur le fichier Accessign sur les fichiers APK d'origine
ce n'est pas bloqué, mais cela a changé. Voir
gettaplicationInfo (). Publicsourcedir code>
getApplicationInfo (). Publicsourcedir
Envoyez-vous APK dans votre application?