java


Android Studio : How to set an external file(photo from the camera) to an imageview


Having some trouble figuring out how to set the imageview to the picture I just captured with the camera. Would be a bonus if there was some way to display multiple captured pictures at once. Whenever I click the button, a previous image captured appears, then the camera opens, which isn't right. Id like the imageview to be blank, I click the button, take a picture, then that picture is displayed in the imageview. I believe that this line is out of place, but i'm unsure as to how/ where to move it. mimageView.setImageURI(outputFileUri);
public class cameraclass extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.camera);
final ImageView mimageView;
mimageView = (ImageView) this.findViewById(R.id.image_from_camera);
// Here, we are making a folder named picFolder to store
// pics taken by the camera using this application.
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/picFolder/";
File newdir = new File(dir);
newdir.mkdirs();
Button capture = (Button) findViewById(R.id.take_image_from_camera);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Here, the counter will be incremented each time, and the
// picture taken by camera will be stored as 1.jpg,2.jpg
// and likewise.
count++;
String file = dir+count+".jpg";
File newfile = new File(file);
try {
newfile.createNewFile();
}
catch (IOException e)
{
}
//Uri outputFileUri = Uri.fromFile(newfile);
Uri outputFileUri = FileProvider.getUriForFile(getApplicationContext() , "com.example.android.com220finalapp.provider", newfile);
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
mimageView.setImageURI(outputFileUri);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
}
}
}
I believe that this line is out of place, but i'm unsure as to how/ where to move it.
startActivityForResult() is asynchronous. Your photo will not have been taken by the time that method returns. You need to load the image into the ImageView in onActivityResult(), if you got a RESULT_OK response.
However, while setImageURI() may work, it has never been an especially good idea, as it will freeze your app for a while as it loads the photo. There are many image loading libraries for Android that will handle loading your ImageView asynchronously.

Related Links

File Name checking
java.lang.AbstractMethodError deploying an application (Spring 4 MVC+Hibernate 4/5)
Tween Engine, how to move object in one direction indefinitely
Output of joining 2 arrays does not come as expected
How to make static images visible in Spring MVC application?
EclipseLink: Map of key to entity where the PK of the entity is the key in the map
Tomcat for Windows Automated setup
Why doesn't my program not registering the value of the sting variable? [duplicate]
Android Studio. Align multiple text boxes under multiple check boxes and equal spacing to fit screen
I'm getting this error in eclipse while using selenium Jar files:
Java order TreeMap by value descending using Collections.reverseOrder()
view is repeating multiple times in listview
Replace occurence with its number (look and say numbers as a regex in Java)
Does the Java JLS specify promotion of primitive wrapper types?
Make random quiz app using array
Why is this int comparison failing? [closed]

Categories

HOME
hive
wso2
listview
openmp
webpack
azure-data-factory
fluentd
electron
gis
analysis
infragistics
frameworks
disassembler
yum
basic
facebook-messenger-bot
elasticsearch-hadoop
user-input
spring-kafka
quicklook
mapserver
qt-creator
correlation
event-handling
systemc
tostring
google-cloud-speech
textfield
jasonette
arabic
kvc
rundeck
crystal-reports-2010
vlsi
javacv
rst2pdf
buck
tasklet
microsoft-chart-controls
http-status-code-503
uisplitview
fog
winrt-xaml-toolkit
subset-sum
uiswipegesturerecognizer
plsql-psp
xcode-extension
libvpx
ansible-playbook
bootstrapper
greenrobot-eventbus
ncalc
g-code
quadratic-programming
password-encryption
websphere-mq-fte
fault
komodoedit
python-c-api
hendrix
typescript1.8
domain-model
dropbox-php
composite-key
font-size
messenger
vmware-tools
bonobo
home-directory
trash
autorest
bstr
react-native-listview
python-stackless
apachebench
pervasive-sql
ios8-today-widget
jmeter-maven-plugin
muse
ios9.1
nsight
relocation
sgen
jquery-layout
datagridviewcolumn
hyprlinkr
oam
dotnetnuke-5
jplaton
batterylevel
buster.js
xamlparseexception
android-screen-support
flashvars
robotics-studio
dice
javaspaces
onsubmit
nsmanagedobject
postgresql-performance
stage
data-loss
zpt
mod-auth
servicehost
pyinotify
sproutcore-2
web-architecture
backcolor

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App