2014-02-10 9 views
0

나는 이미지Viewpager 이미지를 저장하고 공유하는 방법은 무엇입니까?

ImageAdapter에게

public class ImageAdapter extends PagerAdapter { 
Context context; 
private int[] GalImages = new int[] {  
R.drawable.pic_1, 
R.drawable.pic_2, 
R.drawable.pic_3 
. 
. 
. 
}; 
ImageAdapter(Context context){ 
this.context=context; 
} 
@Override 
public int getCount() { 
return GalImages.length; 
} 
@Override 
public boolean isViewFromObject(View view, Object object) { 
return view == ((ImageView) object); 
} 
@Override 
public Object instantiateItem(ViewGroup container, int position) { 
ImageView imageView = new ImageView(context); 
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 
imageView.setImageResource(GalImages[position]); 
((ViewPager) container).addView(imageView, 0); 
return imageView; 
} 
@Override 
public void destroyItem(ViewGroup container, int position, Object object) { 
((ViewPager) container).removeView((ImageView) object); 
} 
} 

MainAcitivtiy 클래스를 저장/I 다음은 지금까지 해봤지만, 아직 내가 공유 할 수 없습니다 Viewpager

의 이미지

을 저장하고 공유 할

public class MainActivity extends Activity { 


Bitmap bm; 
String extStorageDirectory; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager); 
ImageAdapter adapter = new ImageAdapter(this); 
viewPager.setAdapter(adapter); 




extStorageDirectory = Environment.getExternalStorageDirectory().toString(); 
} 


public void share(View v){ 
    Intent share = new Intent(Intent.ACTION_SEND); 
    share.setType("image/*"); 
ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 

bm.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 
    File f = new File(Environment.getExternalStorageDirectory() + File.separator+ "temporary_file.jpg"); 

    try { 
     f.createNewFile(); 
     FileOutputStream fo = new FileOutputStream(f); 
     fo.write(bytes.toByteArray()); 
    } catch (IOException e) {      
      e.printStackTrace(); 
    } 
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///sdcard/temporary_file.jpg")); 
    startActivity(Intent.createChooser(share, "Share Image")); 


    } 

public void save (View v){ 

     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
     FileOutputStream outp = null; 
     File filename; 
     try { 
      String path = Environment.getExternalStorageDirectory().getAbsolutePath()+Nameoffolder; 

      new File(path + "/folder/subfolder").mkdirs(); 
      filename = new File(path + "/folder/subfolder/image.jpg"); 

      FileOutputStream out = new FileOutputStream(filename); 
      out.write(bytes.toByteArray()); 

      bm.compress(Bitmap.CompressFormat.JPEG, 90, out); 
      out.flush(); 
      out.close(); 

      MediaStore.Images.Media.insertImage(getContentResolver(), filename.getAbsolutePath(), filename.getName(), filename.getName()); 

      Toast.makeText(getApplicationContext(), " Saved " , Toast.LENGTH_SHORT).show(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

       } 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

XML 파일

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainActivity" > 

<android.support.v4.view.ViewPager 
android:id="@+id/view_pager" 
android:layout_width="match_parent" 
android:layout_height="0dp" 
android:layout_weight="1" 
android:layout_alignParentTop="true" 
/> 

<Button 
android:id="@+id/button1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignBottom="@+id/view_pager" 
android:layout_alignLeft="@+id/view_pager" 
android:layout_marginBottom="29dp" 
android:text="@string/save" 
android:onClick="save" /> 

<Button 
android:id="@+id/button2" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_alignBaseline="@+id/button1" 
android:layout_alignBottom="@+id/button1" 
android:layout_alignRight="@+id/view_pager" 
android:text="@string/share" 
android:onClick="share" /> 


</RelativeLayout> 

아무도 나를 도울 수 있다면 좋을 것입니다. 고마워요!

답변

0

공유 방법에 대해서는 링크를 통해 이미지를 sdcard this 링크에 저장하면 문제를 해결하는 데 도움이됩니다.

편집 :

이 (조각 자체) 호출기 조각에 공유 및 저장 코드를 작성합니다. 당신이 saveImage()와 shareImage() 당신의 조각에 방법을 쓴 가정 해 봅시다

이제 활동에서 현재의 단편을 얻을

YourPagerFrament FR = (YourPagerFrament) yourPagerAdapter.instantiateItem (호출기, pager.getCurrentItem()) ;

지금 (

fr.saveImage으로 조각에서 메소드를 호출) fr.shareImage();

는 지금

+0

덕분에 이해를 희망하지만 내 편집 대답을 확인 viewpager – user3288587

+0

그것을 수행하는 방법에 도움이 나던. – NullPointerException

+0

미스터 Rashmi에 대한 도움을 주셔서 감사합니다하지만 난 이해가 안 돼요 hv 트랙을 잃었습니다. – user3288587

관련 문제