Reputation: 2678
In my app, I am taking a shot and saving it using the built-in Camera
app. Then, after taking the picture, my app return back to a blank Activity
. All that works.
Now instead of saving and going back to an empty Activity
, I want to view the taken shot on a Webview
or an Imageview
how to do that ?
I start CameraAdapter
class from the MainActivity
class as follow:
public void takePicture(View view) {
Intent cameraIntent = new Intent(this, CameraAdapter.class);
startActivity(cameraIntent);
}
.. and here is the CameraAdapter
class:
public class CameraAdapter extends Activity {
static Uri imageUri;
static File imageFolderPath;
int TAKE_PHOTO_CODE = 0;
String imageFileName;
ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.imgs);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getUri());
startActivityForResult(intent, TAKE_PHOTO_CODE);
ImageView imageV = (ImageView) findViewById(imageView);
String filename = getImageFileName();
Bitmap image = getImage(filename);
imageV.setImageBitmap(image);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(),
"Capture is saved to:\n" + imageFolderPath.toString(),
Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(),
"You cancelled the capture",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"Capture failed",
Toast.LENGTH_SHORT).show();
}
}
public Uri getUri(){
imageFolderPath = new File(Environment.getExternalStorageDirectory() + "/mst_imgs");
if(!imageFolderPath.exists())
imageFolderPath.mkdir();
String imageFullName = imageFolderPath + "/" + getImageFileName();
File newPic = new File(imageFullName);
imageUri = Uri.fromFile(newPic);
return imageUri;
}
private Bitmap getImage(String imageFileName) {
File root = Environment.getExternalStorageDirectory();
//return BitmapFactory.decodeFile(root + "/mst_imgs/" + imageFileName);
return BitmapFactory.decodeFile(root + "/mst_imgs/CS_TEST.jpg");
}
private String getImageFileName(){
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'_'HHmmss");
String timeStamp = dateFormat.format(new Date());
//imageFileName = "CS_" +timeStamp + ".jpg";
imageFileName = "CS_TEST.jpg";
return imageFileName;
}
UPDATE I changed the code a little :
Also, here is the imgs
xml layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/imgs_webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btnShareImage"
android:text="Share ->"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Thank you
Upvotes: 1
Views: 1219
Reputation: 246
You need to return the filename to previews activity, using your intent extra
, with your timestamp and decode
the file to Bitmap, like that:
private Bitmap getImage(String imageFileName) {
File root = Environment.getExternalStorageDirectory();
return BitmapFactory.decodeFile(root + "/mst_imgs/" + imageFileName);
}
Create a ImageView
into the layout.xml
. Insert in your onCreate()
:
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(getImage());
Don't forget to add access for read files into the Manifest.xml
:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
---------- Update Code -----------
I don't know what's happening, I'm passing below my test:
MainActivity
public class MainActivity extends AppCompatActivity {
private File imageFolderPath;
private int TAKE_PHOTO_CODE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, getUri());
startActivityForResult(intent, TAKE_PHOTO_CODE);
ImageView imageV = (ImageView) findViewById(imageView);
String filename = getImageFileName();
Bitmap image = getImage(filename);
imageV.setImageBitmap(image);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Toast.makeText(getApplicationContext(),
"Capture is saved to:\n" + imageFolderPath.toString(),
Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(),
"You cancelled the capture",
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(),
"Capture failed",
Toast.LENGTH_SHORT).show();
}
}
public Uri getUri(){
imageFolderPath = new File(Environment.getExternalStorageDirectory() + "/mst_imgs");
if(!imageFolderPath.exists())
imageFolderPath.mkdir();
String imageFullName = imageFolderPath + "/" + getImageFileName();
File newPic = new File(imageFullName);
Uri imageUri = Uri.fromFile(newPic);
return imageUri;
}
private Bitmap getImage(String imageFileName) {
File root = Environment.getExternalStorageDirectory();
//return BitmapFactory.decodeFile(root + "/mst_imgs/" + imageFileName);
return BitmapFactory.decodeFile(root + "/mst_imgs/CS_TEST.jpg");
}
private String getImageFileName(){
String imageFileName;
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMdd'_'HHmmss");
String timeStamp = dateFormat.format(new Date());
//imageFileName = "CS_" +timeStamp + ".jpg";
imageFileName = "CS_TEST.jpg";
return imageFileName;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/imgs_webview"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/btnShareImage"
android:text="Share ->"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rodrigo.teste">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Upvotes: 1
Reputation: 1536
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Uri picUri = data.getData();
Bitmap bitmap = getThumbnail(imageUri);
imageView.setImageBitmap(bitmap);
}
public static Bitmap getThumbnail(Uri uri) throws FileNotFoundException, IOException{
InputStream input = this.getContentResolver().openInputStream(uri);
BitmapFactory.Options onlyBoundsOptions = new BitmapFactory.Options();
onlyBoundsOptions.inJustDecodeBounds = true;
onlyBoundsOptions.inDither=true;//optional
onlyBoundsOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional
BitmapFactory.decodeStream(input, null, onlyBoundsOptions);
input.close();
if ((onlyBoundsOptions.outWidth == -1) || (onlyBoundsOptions.outHeight == -1))
return null;
int originalSize = (onlyBoundsOptions.outHeight > onlyBoundsOptions.outWidth) ? onlyBoundsOptions.outHeight : onlyBoundsOptions.outWidth;
double ratio = (originalSize > THUMBNAIL_SIZE) ? (originalSize / THUMBNAIL_SIZE) : 1.0;
BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
bitmapOptions.inSampleSize = getPowerOfTwoForSampleRatio(ratio);
bitmapOptions.inDither=true;//optional
bitmapOptions.inPreferredConfig=Bitmap.Config.ARGB_8888;//optional
input = this.getContentResolver().openInputStream(uri);
Bitmap bitmap = BitmapFactory.decodeStream(input, null, bitmapOptions);
input.close();
return bitmap;
}
private static int getPowerOfTwoForSampleRatio(double ratio){
int k = Integer.highestOneBit((int)Math.floor(ratio));
if(k==0) return 1;
else return k;
}
Upvotes: 0
Reputation: 500
You now have the file saved to the device, great, what you want to do now is just set it to an image view.
If you're using bitmaps you'll need to use the BitmapFactory and do some other things to take the file and turn it into something that can be set on an imageview, then worry about loading the correct size and so on.
There are plenty of libraries you can use to handle this for you. But I'd say your best option is to just use Picasso - you can hand it a File and it will load and display it. I use this and it makes life way easier.
Picasso.with(context).load(new File(fileName)).into(imageView);
You can also use the file path if it doesn't just load your file straight away.
Hope this helps, and do some more googling! :)
Upvotes: 1