2016-09-13 2 views
0

Ceci est mon code où je suis en train d'envoyer l'image à whtsapp en utilisant l'intention, mais il donne fichier non trouvé exceptionmise à jour WhatsApp récente doesnt permettent d'envoyer l'image par l'intention

Uri uri = Uri.parse("android.resource://com.company.demoapp/" + resId); 
Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_SEND); 
intent.putExtra(Intent.EXTRA_TEXT, "this is a new url"); 
intent.setType("text/plain"); 
intent.putExtra(Intent.EXTRA_STREAM,uri); 
intent.setType("image/jpeg"); 
intent.setPackage("com.whatsapp"); 
mContext.startActivity(intent); 
+0

Assurez-vous que votre chemin de fichier est correct et le fichier est existe ou non. –

+0

@md gouse avez-vous essayer ma réponse –

+0

@PriyankPatel chemin de fichier est correctement défini –

Répondre

0

Shymaa Othman Answer here working fine as you want

Vous devez donner l'autorisation aussi dans Mainfest android

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 

aussi pour Android 6.0 et plus besoin d'une autorisation d'exécution de WRITE_EXTERNAL_STORAGE

private static final int REQUEST_RUNTIME_PERMISSION = 123; 

if (CheckPermission(youactivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) { 

     shareImageWhatsApp(); 


    } else { 
     // you do not have permission go request runtime permissions 
     RequestPermission(AccesspointCheck.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION); 
    } 




public void RequestPermission(Activity thisActivity, String Permission, int Code) { 
    if (ContextCompat.checkSelfPermission(thisActivity, 
      Permission) 
      != PackageManager.PERMISSION_GRANTED) { 
    if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
      Permission)) { 
     shareImageWhatsApp(); 


    } else { 
     ActivityCompat.requestPermissions(thisActivity, 
       new String[]{Permission}, 
       Code); 
    } 
    } 
} 

public boolean CheckPermission(Activity context, String Permission) { 
    if (ContextCompat.checkSelfPermission(context, 
      Permission) == PackageManager.PERMISSION_GRANTED) { 
    return true; 
    } else { 
    return false; 
    } 
} 

appel shareImageWhatsApp en classe

public void shareImageWhatsApp() { 

    Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.launcher_icon); 
    Intent share = new Intent(Intent.ACTION_SEND); 
    share.setType("image/jpeg"); 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
    adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
    File f = new File(Environment.getExternalStorageDirectory() 
      + File.separator + "temporary_file.jpg"); 
    try { 
    f.createNewFile(); 
    new FileOutputStream(f).write(bytes.toByteArray()); 
    } catch (IOException e) { 
    e.printStackTrace(); 
    } 
    share.putExtra(Intent.EXTRA_STREAM, 
      Uri.parse(Environment.getExternalStorageDirectory()+ File.separator+"temporary_file.jpg")); 
    if(isPackageInstalled("com.whatsapp",this)){ 
    share.setPackage("com.whatsapp"); 
    startActivity(Intent.createChooser(share, "Share Image")); 

    }else{ 

    Toast.makeText(getApplicationContext(), "Please Install Whatsapp", Toast.LENGTH_LONG).show(); 
    } 

} 

private boolean isPackageInstalled(String packagename, Context context) { 
    PackageManager pm = context.getPackageManager(); 
    try { 
    pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES); 
    return true; 
    } catch (PackageManager.NameNotFoundException e) { 
    return false; 
    } 
} 
+0

ce qui est REQUEST_RUNTIME_PERMISSION que vous avez passé dans les paramètres. –

+0

private static final int REQUEST_RUNTIME_PERMISSION = 123; –