Gallery intent in Fragment

  • 2 Antworten
  • Letztes Antwortdatum
R

Rackor

Neues Mitglied
0
Hallo

Ich habe zurzeit paar Probleme mit meinem Viewpager. In diesem habe ich mehrere Fragmente um alle Fotos eines Users anzuzeigen..
das erste Foto wird bereits vorab übergeben. nun ist mein ziel, bei "onLongClick" auf ein Fragment, die gallery zu öffnen, dort ein bild auszuwählen und es im Fragment anzuzeigen. soweit so gut. gallery öffnen mit intent funktioniert...nur weiß ich leider nicht, wie ich die Antwort des gallery intents bekomme, hier weiß ich einfahc nicht weiter =/
Hier der Quellcode.
Anfangs dachte ich ja, da lediglich das aktualisieren der ImageView des Fragments das problem sei, wodurch im Quellcode mehrere Versuche des Aktualisierens vorhanden sind..bis ich dann bemerkte dass das problem schon beim empfangen der fotos vom gallery intent liegt..

Der Code für das Fragment:

Code:
public class ScreenSlidePageFragment extends Fragment{

	final static int RESULT_LOAD_IMAGE = 2;
	ArrayList<String> test = new ArrayList<String>();
	Bitmap profilePic;
	ImageView imgView;
	UserProfilePub_viewpager actRef;
	Bitmap bitmap;
	int pos;
	ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>();
	
	public ScreenSlidePageFragment newInstance(Bitmap profilePic, ArrayList<Bitmap> bitmaps, int pos){
		this.bitmaps = bitmaps;
		this.profilePic = profilePic;
		this.pos = pos;
		ScreenSlidePageFragment f = new ScreenSlidePageFragment();
		Bundle bdl = new Bundle();
		bdl.putParcelableArrayList("profilePics", bitmaps);
		bdl.putInt("pos", pos);
		bdl.putParcelable("profilePic", profilePic);
		f.setArguments(bdl);
		return f;
	}
	
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState){
		ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
		imgView = (ImageView) rootView.findViewById(R.id.up_pub_iv_viewpager);
		if(getArguments().getInt("pos") == 0){
			imgView.setImageBitmap((Bitmap) getArguments().getParcelable("profilePic"));
				imgView.setOnLongClickListener(new OnLongClickListener(){
			
					@Override
					public boolean onLongClick(View arg0) {
						loadPic(imgView);
						return false;
					}
					
				});		
			//}
		}
		//imgView.setImageBitmap((Bitmap) getArguments().getParcelableArrayList("profilePics").get(getArguments().getInt("pos")));
		return rootView;
	}
	
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
	}
	
	public void loadPic(ImageView imgView){
		Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
		int vID = imgView.getId();
		intent.putExtra("vID", vID);
		startActivityForResult(intent, RESULT_LOAD_IMAGE);
		Log.e("test2", "test");
	}
	
}

Code für die FragmentActivity:

Code:
public class UserProfilePub_viewpager extends FragmentActivity {
	ArrayList<Bitmap> pics = new ArrayList<Bitmap>();
	UserProfilePub_viewpager actRef;	
	String url="*********************************";
	Bitmap profilePic;
	int pos;

	private static final int NUM_PAGES = 2;
	private ViewPager mPager;
	private PagerAdapter mPagerAdapter;
	
	ImageView imgView;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.viewpager_layout_activity);
		
		Intent intent = getIntent();
		String USID = intent.getExtras().getString("USID");
		profilePic = intent.getExtras().getParcelable("profilePic");
		pics = intent.getParcelableArrayListExtra("profilePics");

		ArrayList<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
		nameValuePair.add(new BasicNameValuePair("USID", USID));

		//AsyncTask, get Pics
		//ViewPager_getAllPics vp_getPics = new ViewPager_getAllPics(url, nameValuePair, actRef);
		//vp_getPics.execute();		
		
		mPager = (ViewPager) findViewById(R.id.pager);
		mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), profilePic, pics);
		mPager.setAdapter(mPagerAdapter);
		
	}
	
	public void onBackPressed(){
		if(mPager.getCurrentItem() == 0){
			super.onBackPressed();
		}else{
			mPager.setCurrentItem(mPager.getCurrentItem() - 1);
		}
	}
	
	public static class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
		ArrayList<Bitmap> pics = new ArrayList<Bitmap>();
		Bitmap profilePic;
		public ScreenSlidePagerAdapter(FragmentManager fm, Bitmap profilePic, ArrayList<Bitmap> pics) {
			super(fm);
			this.pics = pics;
			this.profilePic = profilePic;
		}

		@Override
		public Fragment getItem(int pos){
			return new ScreenSlidePageFragment().newInstance(profilePic, pics, pos);
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return NUM_PAGES;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.user_profile_pub_viewpager, menu);
		return true;
	}
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		super.onActivityResult(requestCode, resultCode, data);
		
		int RESULT_LOAD_IMAGE = 2;
	
		if(requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data){
	
			int vID = data.getExtras().getInt("vID");
			imgView = (ImageView) findViewById(vID);
					
			Uri selectedImage = data.getData();
			String[] filePathColumn = {MediaStore.Images.Media.DATA};
			
			Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
			cursor.moveToFirst();
			int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
			String picturePath = cursor.getString(columnIndex);
			cursor.close();
			findViewById(R.id.up_pub_iv_viewpager);
			
			decodeBitmap decBit = new decodeBitmap(picturePath, imgView);
			decBit.execute();
			//InputImage.setImageBitmap(BitmapFactory.decodeFile(picturePath));
			Log.e("test", "test");
		}
		
	}
	

	public void savePic(Bitmap bitmap){
		pics.add(bitmap);
		
		/*FragmentTransaction fragmTrans = getSupportFragmentManager().beginTransaction();
		fragmTrans.add(new ScreenSlidePageFragment().newInstance(profilePic, pics, pos), "test");
		fragmTrans.detach(new ScreenSlidePageFragment().newInstance(profilePic, pics, pos));
		fragmTrans.attach(new ScreenSlidePageFragment().newInstance(profilePic, pics, pos));
		fragmTrans.commit();
		*/
		mPager.removeAllViews();
		mPagerAdapter.notifyDataSetChanged();
		mPager.setAdapter(mPagerAdapter);
	}	

}


Hier der Code vom AsyncTasks der die Bilder von der gallery anpasst und decodiert
Code:
public class decodeBitmap extends AsyncTask<String, Integer, Bitmap> {
	BitmapFactory.Options options = new BitmapFactory.Options();
	Bitmap profilePic;
	final static byte[] bytes = null;
	String picturePath;
	ImageView InputImage;
	
	public decodeBitmap(String picturePath, ImageView InputImage){
		this.picturePath = picturePath;
		this.InputImage = InputImage;
	}
	
	public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    
		options.inJustDecodeBounds = true;
		BitmapFactory.decodeFile(picturePath, options);
		int imageHeight = options.outHeight;
		int imageWidth = options.outWidth;
		String imageType = options.outMimeType;
		// Raw height and width of image
		final int height = options.outHeight;
		final int width = options.outWidth;
		int inSampleSize = 1;

		if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}
	
	
	public Bitmap decodeSampledBitmapFromFile(String picturePath, int reqWidth, int reqHeight) {

	    // First decode with inJustDecodeBounds=true to check dimensions
	    final BitmapFactory.Options options = new BitmapFactory.Options();
	    options.inJustDecodeBounds = true;
	    BitmapFactory.decodeFile(picturePath, options);

	    // Calculate inSampleSize
	    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

	    // Decode bitmap with inSampleSize set
	    options.inJustDecodeBounds = false;
	    return BitmapFactory.decodeFile(picturePath, options);
	}


	@Override
	protected Bitmap doInBackground(String... params) {        
		return decodeSampledBitmapFromFile(picturePath, 200, 200);
	}
	
	@Override
	protected void onPostExecute(Bitmap bitmap) {
		InputImage.setImageBitmap(bitmap);
		
		UserProfilePub_viewpager test = new UserProfilePub_viewpager();
		test.savePic(bitmap);
	}
	
	
}
 
hmm, das Problem habe ich soeben gelöst...jz weiß ich jedoch trz nicht weiter...ich würde jz gerne das neu gewonnene foto in einem neuen fragment anzeigen lassen, und dieses fragment soll immer an vorletzter position eigneschoben werden..irgendwer ne idee, wie ich fragmente dynamsich nachladen/aktualisieren/hinzufügen kann von der activity aus?
 
kann geschlossen werden!!
 
Zurück
Oben Unten