-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of https://github.com/jborak/charityspotter
- Loading branch information
Showing
20 changed files
with
489 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
275 changes: 275 additions & 0 deletions
275
...id/CharitySpot/app/src/main/java/com/charityspotter/charityspot/ImageChooserActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,275 @@ | ||
package com.charityspotter.charityspot; | ||
|
||
import android.app.Activity; | ||
import android.content.Intent; | ||
import android.net.Uri; | ||
import android.os.Bundle; | ||
import android.support.annotation.NonNull; | ||
import android.util.Log; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.ImageView; | ||
import android.widget.ProgressBar; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.kbeanie.imagechooser.api.ChooserType; | ||
import com.kbeanie.imagechooser.api.ChosenImage; | ||
import com.kbeanie.imagechooser.api.ImageChooserListener; | ||
import com.kbeanie.imagechooser.api.ImageChooserManager; | ||
import com.squareup.picasso.Callback; | ||
import com.squareup.picasso.Picasso; | ||
|
||
import java.io.File; | ||
|
||
/** | ||
* Created by morrisonchang on 11/8/15. | ||
*/ | ||
public class ImageChooserActivity extends Activity implements ImageChooserListener { | ||
|
||
private final static String TAG = "ICA"; | ||
|
||
private ImageView imageViewThumbnail; | ||
|
||
private ImageView imageViewThumbSmall; | ||
|
||
private TextView textViewFile; | ||
private TextView taglisttext; | ||
|
||
private ImageChooserManager imageChooserManager; | ||
|
||
private ProgressBar pbar; | ||
|
||
private String filePath; | ||
|
||
private int chooserType; | ||
|
||
private boolean isActivityResultOver = false; | ||
|
||
private String originalFilePath; | ||
private String thumbnailFilePath; | ||
private String thumbnailSmallFilePath; | ||
ImageTagger itag; | ||
String[] tagResults; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
Log.i(TAG, "Activity Created"); | ||
setContentView(R.layout.activity_image_chooser); | ||
|
||
itag = new ImageTagger(); | ||
|
||
Button buttonTakePicture = (Button) findViewById(R.id.buttonTakePicture); | ||
buttonTakePicture.setOnClickListener(new View.OnClickListener() { | ||
|
||
@Override | ||
public void onClick(View v) { | ||
takePicture(); | ||
} | ||
}); | ||
Button buttonChooseImage = (Button) findViewById(R.id.buttonChooseImage); | ||
buttonChooseImage.setOnClickListener(new View.OnClickListener() { | ||
|
||
@Override | ||
public void onClick(View v) { | ||
chooseImage(); | ||
} | ||
}); | ||
|
||
|
||
|
||
imageViewThumbnail = (ImageView) findViewById(R.id.imageViewThumb); | ||
imageViewThumbSmall = (ImageView) findViewById(R.id.imageViewThumbSmall); | ||
textViewFile = (TextView) findViewById(R.id.textViewFile); | ||
|
||
taglisttext = (TextView) findViewById(R.id.taglisttext); | ||
|
||
pbar = (ProgressBar) findViewById(R.id.progressBar); | ||
pbar.setVisibility(View.GONE); | ||
|
||
} | ||
|
||
private void chooseImage() { | ||
chooserType = ChooserType.REQUEST_PICK_PICTURE; | ||
imageChooserManager = new ImageChooserManager(this, | ||
ChooserType.REQUEST_PICK_PICTURE, true); | ||
imageChooserManager.setImageChooserListener(this); | ||
imageChooserManager.clearOldFiles(); | ||
try { | ||
pbar.setVisibility(View.VISIBLE); | ||
filePath = imageChooserManager.choose(); | ||
} catch (IllegalArgumentException e) { | ||
e.printStackTrace(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
private void takePicture() { | ||
chooserType = ChooserType.REQUEST_CAPTURE_PICTURE; | ||
imageChooserManager = new ImageChooserManager(this, | ||
ChooserType.REQUEST_CAPTURE_PICTURE, true); | ||
imageChooserManager.setImageChooserListener(this); | ||
try { | ||
pbar.setVisibility(View.VISIBLE); | ||
filePath = imageChooserManager.choose(); | ||
} catch (IllegalArgumentException e) { | ||
e.printStackTrace(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
|
||
@Override | ||
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | ||
Log.i(TAG, "OnActivityResult"); | ||
Log.i(TAG, "File Path : " + filePath); | ||
Log.i(TAG, "Chooser Type: " + chooserType); | ||
if (resultCode == RESULT_OK | ||
&& (requestCode == ChooserType.REQUEST_PICK_PICTURE || requestCode == ChooserType.REQUEST_CAPTURE_PICTURE)) { | ||
if (imageChooserManager == null) { | ||
reinitializeImageChooser(); | ||
} | ||
imageChooserManager.submit(requestCode, data); | ||
} else { | ||
pbar.setVisibility(View.GONE); | ||
} | ||
} | ||
|
||
@Override | ||
public void onImageChosen(final ChosenImage image) { | ||
runOnUiThread(new Runnable() { | ||
|
||
@Override | ||
public void run() { | ||
Log.i(TAG, "Chosen Image: O - " + image.getFilePathOriginal()); | ||
Log.i(TAG, "Chosen Image: T - " + image.getFileThumbnail()); | ||
Log.i(TAG, "Chosen Image: Ts - " + image.getFileThumbnailSmall()); | ||
isActivityResultOver = true; | ||
originalFilePath = image.getFilePathOriginal(); | ||
thumbnailFilePath = image.getFileThumbnail(); | ||
thumbnailSmallFilePath = image.getFileThumbnailSmall(); | ||
pbar.setVisibility(View.GONE); | ||
if (image != null) { | ||
Log.i(TAG, "Chosen Image: Is not null"); | ||
textViewFile.setText(image.getFilePathOriginal()); | ||
loadImage(imageViewThumbnail, image.getFileThumbnail()); | ||
loadImage(imageViewThumbSmall, image.getFileThumbnailSmall()); | ||
new Thread() { | ||
public void run() { | ||
tagResults = itag.getTag(new File(image.getFilePathOriginal())); | ||
} | ||
}.start(); | ||
|
||
StringBuffer outSb = new StringBuffer("Tags: "); | ||
if(tagResults != null) { | ||
for (String e : tagResults) { | ||
outSb.append("'" + e + "' "); | ||
Log.i(TAG, "Tag entry:" + e); | ||
} | ||
} else { | ||
outSb = new StringBuffer("NO TAGS"); | ||
Log.i(TAG,"NO TAGS"); | ||
} | ||
taglisttext.setText(outSb.toString()); | ||
|
||
} else { | ||
Log.i(TAG, "Chosen Image: Is null"); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
private void loadImage(ImageView iv, final String path) { | ||
Picasso.with(ImageChooserActivity.this) | ||
.load(Uri.fromFile(new File(path))) | ||
.fit() | ||
.centerInside() | ||
.into(iv, new Callback() { | ||
@Override | ||
public void onSuccess() { | ||
Log.i(TAG, "Picasso Success Loading Thumbnail - " + path); | ||
} | ||
|
||
@Override | ||
public void onError() { | ||
Log.i(TAG, "Picasso Error Loading Thumbnail Small - " + path); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public void onError(final String reason) { | ||
runOnUiThread(new Runnable() { | ||
|
||
@Override | ||
public void run() { | ||
Log.i(TAG, "OnError: " + reason); | ||
pbar.setVisibility(View.GONE); | ||
Toast.makeText(ImageChooserActivity.this, reason, | ||
Toast.LENGTH_LONG).show(); | ||
} | ||
}); | ||
} | ||
|
||
// Should be called if for some reason the ImageChooserManager is null (Due | ||
// to destroying of activity for low memory situations) | ||
private void reinitializeImageChooser() { | ||
imageChooserManager = new ImageChooserManager(this, chooserType, true); | ||
imageChooserManager.setImageChooserListener(this); | ||
imageChooserManager.reinitialize(filePath); | ||
} | ||
|
||
@Override | ||
protected void onSaveInstanceState(Bundle outState) { | ||
Log.i(TAG, "Saving Stuff"); | ||
Log.i(TAG, "File Path: " + filePath); | ||
Log.i(TAG, "Chooser Type: " + chooserType); | ||
outState.putBoolean("activity_result_over", isActivityResultOver); | ||
outState.putInt("chooser_type", chooserType); | ||
outState.putString("media_path", filePath); | ||
outState.putString("orig", originalFilePath); | ||
outState.putString("thumb", thumbnailFilePath); | ||
outState.putString("thumbs", thumbnailSmallFilePath); | ||
super.onSaveInstanceState(outState); | ||
} | ||
|
||
@Override | ||
protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) { | ||
if (savedInstanceState != null) { | ||
if (savedInstanceState.containsKey("chooser_type")) { | ||
chooserType = savedInstanceState.getInt("chooser_type"); | ||
} | ||
if (savedInstanceState.containsKey("media_path")) { | ||
filePath = savedInstanceState.getString("media_path"); | ||
} | ||
if (savedInstanceState.containsKey("activity_result_over")) { | ||
isActivityResultOver = savedInstanceState.getBoolean("activity_result_over"); | ||
originalFilePath = savedInstanceState.getString("orig"); | ||
thumbnailFilePath = savedInstanceState.getString("thumb"); | ||
thumbnailSmallFilePath = savedInstanceState.getString("thumbs"); | ||
} | ||
} | ||
Log.i(TAG, "Restoring Stuff"); | ||
Log.i(TAG, "File Path: " + filePath); | ||
Log.i(TAG, "Chooser Type: " + chooserType); | ||
Log.i(TAG, "Activity Result Over: " + isActivityResultOver); | ||
if (isActivityResultOver) { | ||
populateData(); | ||
} | ||
super.onRestoreInstanceState(savedInstanceState); | ||
} | ||
|
||
private void populateData() { | ||
Log.i(TAG, "Populating Data"); | ||
loadImage(imageViewThumbnail, thumbnailFilePath); | ||
loadImage(imageViewThumbSmall, thumbnailSmallFilePath); | ||
} | ||
|
||
@Override | ||
public void onDestroy() { | ||
super.onDestroy(); | ||
Log.i(TAG, "Activity Destroyed"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.