2017-04-27 4 views
1
 //Share image to all 
     share = (Button)findViewById(R.id.facebook_app_id); 
     share.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       Uri imageUri = Uri.parse("android.resource://" + getPackageName() +"/drawable/"+imageRes); 
       Intent intent = new Intent(Intent.ACTION_SEND); 
       intent.setType("image/*"); 

       intent.putExtra(Intent.EXTRA_STREAM, imageUri); 
       intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
       startActivity(Intent.createChooser(intent , "Share")); 


      } 

     }); 

사진 공유 앱을 만들려고합니다. 페이스 북, 메신저, 스카이프 작업 완벽하지만 WHATSAPP 및 Viber 표시 오류 (파일 형식이 지원되지 않습니다) 파일 형식이 지원되지 않습니다 - 오류 표시 whatsapp에 사진을 보낼 때

:-(내가. 내가 많이 노력하고 있지만, pls는 Pls는. 작동하지 않는 안드로이드 초보자입니다 . PLS, 나는 확실하지 않다

+0

시도해보십시오. String path = MediaStore.Images.Media.insertImage (getContentResolver(), "YOUR_PATH", "Image Description", null); Uri uri = Uri.parse (path); –

+0

"YOUR_PATH"가 무엇입니까? ""android.resource : // "+ getPackageName() +"/ drawable/"+ imageRes >>를 의미합니까? –

+0

내 코드에 따르면 내 이미지 경로는 "android.resource : //"+ getPackageName() + "/ drawable /"+ imageRes이며 이미지 설명은 무엇입니까? @Divyesh Patel –

답변

0

도와하지만 당신은 예를 android.resource에 대한 .JPEG 또는 PNG 등이 확장자를 가진 imageRes를 확인할 수 있습니다 : // "+ getPackageName() +"/ 드로어 블/사진 "+" imageRes.jpg "

0

콘텐츠 유형을 th 공유 의도는 이미지/png입니다. 위에서 말한대로 모든 앱에서 작동하는지 여부는 다를 수 있습니다.

이유는 당신이 (당신의 응용 프로그램의의 resourses 내부 저장 항상있을 것입니다 물론) 내부 저장

이 달성의 두 가지 방법이 있습니다 애플 리케이션에서 직접 URI를 공유 할 수 있습니다 ..

이미지를 외부 저장소에 복사 한 다음 공유하십시오. 이 부분을 참조하십시오

콘텐츠 제공자를 작성하여 이미지를 공유하십시오.

public void shareImageWhatsApp() { 

    Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.adv); 
    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 (NameNotFoundException e) { 
     return false; 
    } 
} 
+0

이미지를 공유하기 위해 외부 저장 장치 및 콘텐츠 제공자에게 이미지 공유에 대한 참조를 줄 수 있습니까? –

+0

http://stackoverflow.com/questions/12170386/create-and-share-a-file-from-internal-storage – albeee

0

다음은이 작업을 수행하는 방법을 예입니다 EXTERNAL_STORAGE 권한을 부여하고 sdk를 lolipop보다 높게 컴파일 한 다음 권한을 요청해야합니다.

+0

아주 나쁨 png 파일을 비트 맵으로 변환 한 다음 스트림으로 압축하고 두 번째 스트림을 사용하여 jpg 파일을 작성합니다. 대신 드로어 블을 직접 파일에 복사 할 수 있습니다. – greenapps

0

이 시도 :이 코드 전에

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher_round); 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos); 
    Intent intent = new Intent(Intent.ACTION_SEND); 
    intent.setType("image/*"); 
    String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, "ic_launcher_round", null); 
    Uri imageUri = Uri.parse(path); 
    intent.putExtra(Intent.EXTRA_STREAM, imageUri); 
    intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
    startActivity(Intent.createChooser(intent , "Share")); 

은 당신이 있는지 확인 실행 즉 내부 저장에서 파일 만들기를 참조 및 공유를 위해

+0

Minmap이 아닌 모든 drawable 디렉토리에서 이미지를 확인하십시오. –

+0

ImageView를 사용합니다 ... ImageView image = (ImageView) findViewById (R.id.imageView2); image.setImageResource (imageRes); ImageAdapter를 사용하면 효과가 있습니까? 내 코드에 따르면 "ic_launcher_round"=? 내 ic_launcher_round는 무엇입니까? –

+0

drawable 디렉토리에있는 이미지 이름 –