0

我正在从服务器加载一组图像并在 ViewPager 中显示。当我点击保存按钮时,下一张图像将保存到 SD 卡,而不是确切的图像。. 这个怎么处理??这是我的代码

public class PhotoDetailsFragment extends SuperFragment implements OnClickListener {
SamplePagerAdapter adapter;
private Button shareBtn ;
private AlbumBean photoData ;
private Button saveImageBtn ;
private ArrayList < String > photosLinks = new ArrayList < String >() ;
ViewPager pager ;
 int currentPage=0;
public ZoomableImageView imageView ;
public int page = 0 ;

@ Override
public View onCreateView( LayoutInflater inflater , ViewGroup container , Bundle savedInstanceState ) {
    return inflater.inflate( R.layout.photo_details_layout , container , false ) ;

}

@ Override
public void onActivityCreated( Bundle savedInstanceState ) {
    // TODO Auto-generated method stub
    super.onActivityCreated( savedInstanceState ) ;


    pager = ( ViewPager ) getActivity().findViewById( R.id.pager ) ;
    init( savedInstanceState ) ;

}

/**
 * init the main components in the layout
 */
private void init( Bundle savedInstanceState ) {
    // TODO Auto-generated method stub

    if ( Util.load( getActivity() , IAAConfig.KEY_GALLERY_LAST_POS ).length() > 0 ) {
        page = Integer.parseInt( Util.load( getActivity() , IAAConfig.KEY_GALLERY_LAST_POS ) ) ;
    } else
        page = getArguments().getInt( IAAConfig.KEY_GALLERY_LAST_POS ) ;

    photoData = ( AlbumBean ) getArguments().getSerializable( IAAConfig.KEY_ALBUM_INFO ) ;

    photosLinks = photoData.getPhotoLinks() ;

    try {
        shareBtn = ( Button ) getActivity().findViewById( R.id.share_btn ) ;
        shareBtn.setOnClickListener( this ) ;

        saveImageBtn = ( Button ) getActivity().findViewById( R.id.saveImageBtn ) ;
        saveImageBtn.setOnClickListener( this ) ;
    } catch ( Exception e ) {
        // TODO: handle exception
    }


    adapter = new SamplePagerAdapter();
    pager.setAdapter( new SamplePagerAdapter() ) ;
    pager.setCurrentItem( page) ;  //adapter.getCount()-1

}

@ Override
public void onClick( View v ) {
    // TODO Auto-generated method stub

    switch ( v.getId() ) {
    case R.id.share_btn:
        Util.shareTextUrl( getActivity() , photosLinks.get( pager.getCurrentItem() ) ) ;
        break ;

    case R.id.saveImageBtn:


           //int t = adapter.getCount()-1;
         //String aString = Integer.toString(t);
        // pager.setCurrentItem(getItem(+1), true);

    Util.saveMyImage( imageView , getActivity() , pager.getCurrentItem()+""  ) ;//adapter.getCount().pager.getCurrentItem()

        //pager.setCurrentItem(pager.getCurrentItem())

        break ;

    default:
       break ;
    }

}



private ContentResolver getContentResolver() {
    // TODO Auto-generated method stub
    return null;
}

private int getItem(int i) {
    // TODO Auto-generated method stub
    return pager.getCurrentItem() + i;
}



class SamplePagerAdapter extends PagerAdapter {

    @ Override
    public int getCount() {
        return photosLinks.size() ;
    }

    @ Override
    public boolean isViewFromObject( View view , Object object ) {
        return view == object ;
    }

    @ Override
    public Object instantiateItem( ViewGroup container , int position ) {

        imageView = new ZoomableImageView( getActivity() ) ;
        imageView.setDrawingCacheEnabled( true ) ;
        imageView.buildDrawingCache( true ) ;
        imageView.setMaxZoom( 10f ) ;
        imageView.destroyDrawingCache();
        try {
            Picasso.with( getActivity() ).load( photosLinks.get( position ) ).into( imageView ) ;

        } catch ( Exception e ) {
            // TODO: handle exception
        }

        LayoutParams imageParams = new LayoutParams( LayoutParams.MATCH_PARENT , LayoutParams.MATCH_PARENT ) ;

        imageView.setLayoutParams( imageParams ) ;
        LinearLayout layout = new LinearLayout( getActivity() ) ;
        layout.setOrientation( LinearLayout.VERTICAL ) ;
        LayoutParams layoutParams = new LayoutParams( LayoutParams.MATCH_PARENT , LayoutParams.MATCH_PARENT ) ;
        layout.setLayoutParams( layoutParams ) ;
        layout.addView( imageView ) ;

        container.addView( layout ) ;
        //pager.setCurrentItem(getItem(+1), true);
        return layout ;

    }

    @ Override
    public int getItemPosition( Object object ) {
        // TODO Auto-generated method stub

        return super.getItemPosition( object ) ;
    }

    @ Override
    public void destroyItem( ViewGroup container , int position , Object object ) {
        container.removeView( ( LinearLayout ) object ) ;
        //pager.removeView((View) object);
    }

}

/*
 * (non-Javadoc)
 * 
 * @see android.app.Fragment#onSaveInstanceState(android.os.Bundle)
 */
@ Override
public void onSaveInstanceState( Bundle outState ) {
    // TODO Auto-generated method stub
    super.onSaveInstanceState( outState ) ;
    //outState.putInt("pageItem", pager.getCurrentItem());
    Util.save( getActivity() , IAAConfig.KEY_GALLERY_LAST_POS ,       pager.getCurrentItem() + "" ) ;
}

这里是 saveMyImage 方法:

 public static void saveMyImage( ImageView imageView , Context context , String imageName ) {

    try {
       Bitmap bmImg = Bitmap.createBitmap( imageView.getDrawingCache() ) ;
        imageView.setDrawingCacheEnabled( false ) ;
        imageView.destroyDrawingCache();
        String fullImagePath = android.os.Environment.getExternalStorageDirectory().toString() + "/" + context.getString( R.string.app_name ) ;
        File filename ;
        File file = new File( fullImagePath ) ;
        if ( !file.exists() )
            file.mkdirs() ;
        filename = new File( file.getAbsolutePath() + "/" + imageName + ".jpg" ) ;
        FileOutputStream out = new FileOutputStream( filename ) ;
        bmImg.compress( Bitmap.CompressFormat.PNG , 90 , out ) ;
        out.flush() ;
        out.close() ;

        ContentValues image = getImageContent( filename , context ) ;
        Uri result = context.getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI , image ) ;
        Toast.makeText( context , "File is Saved in  " + filename , Toast.LENGTH_LONG ).show() ;
        Trace( "File is Saved in  " + filename ) ;




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

}
4

0 回答 0