Suda.nese Suda.nese - 25 days ago 11
Android Question

Android: How to take a camera-shot and view it in Webview/ Imageview afterword?

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

Answer

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>
Comments