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

difference between spy and direct object
Yet another JNI related java.lang.UnsatisfiedLinkError
Validating a form using JSP
Unable to use my method with Slick library
How to produce a executable jar with maven that contains a subset of dependencies?
Magnetic sensor and image rotation in Android
Create dynamic Type Java
Making a method that will call a second from a qualified name [closed]
Java webstart complaining about missing jar that isn't in my library anymore (Netbeans)
printing partitioned set in partition prob
Method in inner class stripped away by proguard
In java socket programming, is it necessary to run server program first before client program?
storing and retrieving data from sqlite database?
Tomcat 5.5: The requested resource is not available [duplicate]
Creating Dictionary in java?
Login authentication programming pattern

Categories

HOME
oracle11g
azure-data-factory
json.net
syntax
vmware
android-4.4-kitkat
ebean
elasticsearch-hadoop
rascal
correlation
php-7.1
phaser
visual-studio-cordova
iron-router
telephony
seaborn
ab-testing
text-rendering
swiftlint
cx-freeze
tokenize
css-animations
p-value
sparse-matrix
spark-jobserver
xacml
objectanimator
tinymce-4
google-rich-snippets
mmenu
mpmediaquery
appfabric
file-format
space-complexity
reportbuilder
user-controls
total-commander
jsch
jedis
powercli
knockout-components
pillow
flow-control
spring-cache
color-profile
blacklist
crosswalk-runtime
objective-c-swift-bridge
setuptools
grails-tomcat-plugin
savon
database-optimization
gridview-sorting
contact-list
connect-by
rvest
netmq
hsv
maven-tomcat-plugin
wso2cloud
lemon
purge
pretty-print
sorl-thumbnail
google-style-guide
article
funcunit
valueconverter
mechanize-ruby
ccss
ril
jplaton
cos
dataadapter
eventual-consistency
random-seed
selected
pendrive
coercion
xmemcached
jmock
xfbml
cxxtest
h.323
blitz++
sproutcore-2
microsoft-virtualization
uimenucontroller
noscript

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