Load Image File to Image View - java

So, I'm (still) trying to build a simple camera app and what I have so far is an Image Controller which is able to take a picture, save it into the storage and pass the filepath with an intent to another activity.
In my new activity the first step I'm tying to achieve is, to get my final Image loaded into an ImageView, so I did the following:
ImageView finalImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_after);
Intent getIntent = getIntent();
String filePath = getIntent.getExtras().getString("filePath");
File imgFile = new File(filePath);
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
finalImage = (ImageView) findViewById(R.id.finalImage);
finalImage.setImageBitmap(myBitmap);
}
But, its not working, and I dont quite unterstand why not... :( The Activity starts completely fine, but the ImageView just does nothing.

There are few things you must know in the process of saving Image in Android
ccv2WithPreview.takePicture();
In this line the method executed is
public void takePicture() {
try {
// This is how to tell the camera to lock focus.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER, CameraMetadata.CONTROL_AF_TRIGGER_START);
// Tell #mCaptureCallback to wait for the lock.
mState = STATE_WAITING_LOCK;
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback, backgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
Toast.makeText(activity.getApplicationContext(), file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
}
In this method, capture() call is asynchronous. Since you are ending the activity soon after calling it and starting new Activity, so by the time your code saves the Image in Image Saver, you are starting new Activity already and your file is not yet ready.
You button implementation works because by the time you click button, Image will be saved.
To solve this problem,
CameraHelper.java
public interface CameraHelper{
void fileSaved(String filePath);
}
MainActivity.java
public class MainActivity extends AppCompatActivity
implements SensorEventListener, ActivityCompat.OnRequestPermissionsResultCallback, CameraHelper{
...
#Override
public void fileSaved(String file){
Intent intent = new Intent(this, AfterActivity.class);
intent.putExtra("filePath", file);
startActivity(intent);
finish();
}
}
in onCreate of MainActivity
ccv2WithPreview = new CameraControllerV2WithPreview(MainActivity.this, textureView, MainActivity.this);
in your camera class
private CameraHelper cameraHelper; //Initialize in constructor
Then in ImageSaver
private boolean imageSaved = false;
public void run() {
if(!imageSaved) {
ByteBuffer buffer = mImage.getPlanes()[0].getBuffer();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
try (FileOutputStream output = new FileOutputStream(mFile)) {
output.write(bytes);
imageSaved = true;
cameraHelper.fileSaved(mFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
} finally {
mImage.close();
}
}
}
This is how you must handle any Asynchronous tasks you get in future. Callback based implementations.

Can you try to invalidate() the ImageView after setting the bitmap?
(I'm pretty much guessing here, but working with GUIs a couple of years ago, i remember that if a view changed you have to tell the view to about it, so that it can be redrawn.)
Please also see: How to refresh image view immediately

Related

onProgressUpdate() for AsyncTask class doesn't work

I want to show progressbar where image downloaded and set custom color
I do it in onProgressUpdate() but it dosent work it also doesn't appear in logcat.. it also shows a white screen until download completed and if I press back button during the download, it will crash.
my code:
public class DownloadImage extends AsyncTask<String ,Void, Bitmap> {
Bitmap bit;
#Override
protected Bitmap doInBackground(String... urls) {
try {
URL url = new URL(urls[0]);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
return BitmapFactory.decodeStream(connection.getInputStream());
} catch(Exception e){
Log.i("error download", "doInBackground: "+e.getMessage());
}
return null;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
Log.i("download", "onPostExecute: ");
imageView.setImageBitmap(bitmap);
progressBar.setVisibility(View.GONE);
}
#Override
protected void onProgressUpdate(Void... values) {
Log.i("download", "onProgressUpdate: ");
imageView.setColorFilter(R.color.imagecolor);
}
}
and onCreate() method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
imageView = findViewById(R.id.imageView2);
progressBar = findViewById(R.id.progressBar2);
DownloadImage downloadImage = new DownloadImage();
downloadImage.execute("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRaL6woz3RgMF-UXU682S_BYb1ayl5xaVancp0PPvF2HnCDmPsb");
try {
downloadImage.get();
} catch (Exception e){
}
}
I want to show progressbar where image downloaded and set custom color I do it in onProgressUpdate() but it dosent work
You need to call publishProgress() from doInBackground(). That will trigger calls to onProgressUpdate(). You are not doing this, and so onProgressUpdate() will not be called.
it also shows a white screen until download completed
Remove your downloadImage.get(); call. That will block the main application thread, and the point of using AsyncTask (or its more modern replacements) is to not block the main application thread.
and if I press back button during the download, it will crash.
If the activity/fragment is destroyed, you should not update the UI. So, you need to confirm in onPostExecute() whether it is safe to update the UI (e.g., call isDestroyed() on the activity).
Beyond that, use Logcat to examine the stack trace associated with any crashes.

Is there a way to use multiple sources for the same ImageView?

I'm trying to use an Activity which displays a random object from my array. This object is passed in from an intent.
I am trying to use an image for each of these objects and then display the correct image for the correct object.
So far I've been using the drawable folder to hold my images and then loading them in through the XML however this stops me using multiple images for the same ImageView.
I tried using imageview.setImageResource(R.drawable.imagename); but that doesn't seem to like loading in for some reason.
Do I need to make a new activity for each of the objects in this case?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_random_race);
TextView name = (TextView)findViewById(R.id.raceName);
Intent secondIntent = getIntent();
Race message = (Race)secondIntent.getSerializableExtra("RACE");
ImageView image = (ImageView) findViewById(R.id.raceImage);
image.setImageResource(R.drawable.hacan);
image.setImageBitmap(imageToBitmapImage(message, image));
name.setText(message.getName());
}
Bytes to Bitmap method
public Bitmap imageToBitmapImage (Race message, ImageView image){
Bitmap bmp;
try {
FileInputStream in = new FileInputStream(message.getImageName());
BufferedInputStream buffer = new BufferedInputStream(in);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int input = buffer.read();
while (input != -1){
baos.write(input);
input = buffer.read();
}
byte[] bytes = baos.toByteArray();
bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bmp;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Class of each object I'm talking about.
public class Race implements Serializable {
private String name;
private String imageName; //name of file within drawable
As #XavierFalempin commented, you can't access ressources through a file stream. Using setImageResource() should work. Following this answer your onCreate() method should look something like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_random_race);
TextView name = (TextView)findViewById(R.id.raceName);
Intent secondIntent = getIntent();
Race message = (Race)secondIntent.getSerializableExtra("RACE");
ImageView image = (ImageView) findViewById(R.id.raceImage);
image.setImageResource(getResources().getIdentifier(message.getImageName(),
"drawable",
getPackageName()));
name.setText(message.getName());
}

Tap to Save Photo Android

I am pretty new to Android development and I'm trying to figure out how to tap an image in my app and save it to the device. When the image is tapped I want a Save button to appear and when that is pressed, a toast should appear saying the picture was saved. On iOS I am able to do this with UIActionSheet.
I should also mention that the image view image is downloaded from a URL using Picasso.
I just tried this and it says the image saved but when I go to the photos app on my phone, the image is not there.
largeImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
saveButton.setVisibility(View.VISIBLE);
}
});
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
largeImage.getDrawable();
Bitmap bitmap = ((BitmapDrawable)largeImage.getDrawable()).getBitmap();
OutputStream outStream = null;
File file = new File(storageDirectory, "er.PNG");
try {
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
Toast.makeText(FlickrImageActivity.this, "Saved", Toast.LENGTH_LONG).show();
}
catch (FileNotFoundException e) {
e.printStackTrace();
Toast.makeText(FlickrImageActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
catch (IOException e) {
e.printStackTrace();
Toast.makeText(FlickrImageActivity.this, e.toString(), Toast.LENGTH_LONG).show();
}
saveButton.setVisibility(View.GONE);
}
});
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
Random generator = new Random();
int n = 10000;
n = generator.nextInt(n);
String fname = "Image-"+ n +".jpg";
File file = new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Manifest permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
You can use RelativeLayout to contain a ImageView and a Button.
Align both of them center in parent
inside the activity, set the button visibility to "gone"
call setOnClickListener of the ImageView and Button to your Activity
implement OnClickListener in your Activity
inside onClick(View v), if the view clicked is the ImageView, set the visibility of Button to "visible"
inside onClick(View v), if the view clicked is the Button, save the image to disk
** how to save image to disk
if the image source is drawable resources, use BitmapFactory.decodeResource to create a Bitmap then use Bitmap.compress to export to a specific path
specific path is recommended to be obtained from Environment.getExternalStoragePublicDirectory
then notify the Android to refresh gallery
MediaScannerConnection.scanFile(context,
new String[] { imagePath }, null,
new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
//....
}
});
Then use a Toast.makeText(context, "message here").show(); to show the message to user
See below steps to acheive this in Android :
1. Create layout with ImageView & 'Save' named Button
2.By deafult set 'Save' Button's visibility = gone/invisible
3. Apply click listener on both the views (ImageView & Button)
4. OnClick of ImageView, set 'Save' Button's visibility = visible
5. onclick of save button click call your save image to sdcard logic. Check below link for that.
http://android-er.blogspot.in/2010/07/save-file-to-sd-card.html
Hope this will help you.

Android Setting Up Splash Screen(Activity) Like Iphone Part1

I have three images with me and i want them to appear on first layout xml like a splash view so that they can be viewed only once i.e that activity will be called only once when app get's installed or if app get's a new update otherwise app should always start from the Second activity, i don't know how should i begin with this :
Can any one tell me any idea how this can be done.
To show splash for only once.
Next part of this question is here
Coding will be much appreciated.
Save a flag in the Preferences when you start up the application, after you've done the welcome screen stuff. Check for this flag before you show the welcome screen. If the flag is present (in other words, if it's not the first time), don't show it.
In your activity:
SharedPreferences mPrefs;
final String welcomeScreenShownPref = "welcomeScreenShown";
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
// second argument is the default to use if the preference can't be found
Boolean welcomeScreenShown = mPrefs.getBoolean(welcomeScreenShownPref, false);
if (!welcomeScreenShown) {
// here you can launch another activity if you like
// the code below will display a popup
String whatsNewTitle = getResources().getString(R.string.whatsNewTitle);
String whatsNewText = getResources().getString(R.string.whatsNewText);
new AlertDialog.Builder(this).setIcon(android.R.drawable.ic_dialog_alert).setTitle(whatsNewTitle).setMessage(whatsNewText).setPositiveButton(
R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
}).show();
SharedPreferences.Editor editor = mPrefs.edit();
editor.putBoolean(welcomeScreenShownPref, true);
editor.commit(); // Very important to save the preference
}
}
Try this :
public class MainActivity extends Activity {
private Thread mSplashThread;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.splash);
final MainActivity sPlashScreen = this;
mSplashThread = new Thread() {
#Override
public void run() {
try {
synchronized (this) {
wait(4000);
}
} catch (InterruptedException ex) {
}
finish();
Intent intent = new Intent();
intent.setClass(sPlashScreen, StartNewActivity.class);// <-- Activity you want to start after Splash
startActivity(intent);
}
};
mSplashThread.start();
} catch (Exception e) {
}
}
#Override
public boolean onTouchEvent(MotionEvent evt) {
try {
if (evt.getAction() == MotionEvent.ACTION_DOWN) {
synchronized (mSplashThread) {
mSplashThread.notifyAll();
}
}
} catch (Exception e) {
}
return true;
}
}
you put an Image in splash.xml to show
to do this you have to detect the first launch of your application. To do so you can store a boolean value as #Nirav suggested.
And for the splash screen, You can consider using Fragments and ViewPager to create an activity which will only be shown for the first time

Is there a way to call a class function from another class directly without an instance of it in Java?

I have an application that I have taking a picture, and then it is supposed to send the data from the picture to another activity using an intent.
I am trying to call the intent inside the jpegCallback, but the problem is I also need to release the camera through the preview class before calling the intent. However, I can not get to the original preview object from inside the callback, so I need a way to call MainActivity.doPictureResults() from inside the callback. Or I need a way to have a listener that fires after all of the picture callbacks are done.
Here is my MainActivity class which holds an instance of Preview class in the mPreview variable. The jpegCallback is at the bottom, and I want to call the doPictureResults from inside that, or setup another callback for after that function is done.
public class MainActivity extends Activity {
private final String TAG = "MainActivity";
private Preview mPreview;
Camera mCamera;
int numberOfCameras;
int cameraCurrentlyLocked;
//The first rear facing camera
int defaultCameraId;
/**
* Constructor
* #param savedInstanceState
*/
#Override
protected void onCreate(Bundle savedInstanceState) {Log.e(TAG, "onCreate");
super.onCreate(savedInstanceState);
//Hide the window title.
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
//Create a RelativeLayout container that will hold a SurfaceView,
//and set it as the content of our activity.
this.mPreview = new Preview(this);
setContentView(this.mPreview);
//Find the total number of cameras available
this.numberOfCameras = Camera.getNumberOfCameras();
//Find the ID of the default camera
CameraInfo cameraInfo = new CameraInfo();
for(int i = 0; i < this.numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if(cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
this.defaultCameraId = i;
}
}
}
#Override
protected void onResume() {Log.e(TAG, "onResume");
super.onResume();
//Open the default i.e. the first rear facing camera.
this.mCamera = Camera.open();
this.cameraCurrentlyLocked = this.defaultCameraId;
this.mPreview.setCamera(mCamera);
}
#Override
protected void onPause() {Log.e(TAG, "onPause");
super.onPause();
//Because the Camera object is a shared resource, it's very
//Important to release it when the activity is paused.
this.mPreview.releaseCamera();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate our menu which can gather user input for switching camera
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.camera_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//Handle item selection
switch (item.getItemId()) {
case R.id.switchCam:
//Check for availability of multiple cameras
if(this.numberOfCameras == 1) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(this.getString(R.string.camera_alert)).setNeutralButton("Close", null);
AlertDialog alert = builder.create();
alert.show();
return true;
}
//OK, we have multiple cameras.
//Release this camera -> cameraCurrentlyLocked
this.mPreview.releaseCamera();
//Acquire the next camera and request Preview to reconfigure parameters.
this.mCamera = Camera.open((this.cameraCurrentlyLocked + 1) % this.numberOfCameras);
this.cameraCurrentlyLocked = (this.cameraCurrentlyLocked + 1) % this.numberOfCameras;
this.mPreview.switchCamera(mCamera);
//Start the preview
this.mCamera.startPreview();
return true;
case R.id.takePicture:
this.mCamera.takePicture(null, null, jpegCallback);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void doPictureResults(byte[] data) {
this.mPreview.releaseCamera();
//Release the camera and send the results of the image to the GetResults view
Intent resultsIntent = new Intent(MainActivity.this, ImageProcessorActivity.class);
resultsIntent.putExtra("image_data", data);
startActivity(resultsIntent);
}
/**
* Handles data for jpeg picture when the picture is taken
*/
PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera mCamera) {Log.e(TAG, "jpegCallback");
String baseExternalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = String.format("Assist/%d.jpg", System.currentTimeMillis());
FileOutputStream outStream = null;
try {
//Create the directory if needed
File assistDirectory = new File(baseExternalDir + File.separator + "Assist");
assistDirectory.mkdirs();
// Write to SD Card
outStream = new FileOutputStream(baseExternalDir + File.separator + fileName);
outStream.write(data);
outStream.close();
}
catch (FileNotFoundException e) {
Log.e(TAG, "IOException caused by PictureCallback()", e);
}
catch (IOException e) {
Log.e(TAG, "IOException caused by PictureCallback()", e);
}
//This is the type of thing I WANT to do....but its not possible.
MainActivity.doPictureResults();
}
};
}
One options would be to create a PictureCallback implementation that saved the information was required in doPictureResults. It's not clear if doPictureResults will be called anywhere else; if it's not, this is clean and isolates the functionality.
Another would be to have the activity itself implement PictureCallback so you have direct access to all the member variables without having to do any work at all. This allows doPictureResults to be called from other places.
public class MainActivity extends Activity implements PictureCallback {
...
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
....
case R.id.takePicture:
this.mCamera.takePicture(null, null, this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
...
public void onPictureTaken(byte[] data, Camera mCamera) {
Log.d(TAG, "jpegCallback");
String baseExternalDir = Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
...
doPictureResults();
}
}
The only methods you can call on a class without an instance are static methods, but I don't know if that will do what you want here.

Categories