bitmapFactory.decodefile giving null as output - java

package com.example.sugandhabansal.gallerypickimage;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private static final int SELECTED_PIC = 1;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
}
public void btnClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, SELECTED_PIC);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case SELECTED_PIC:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String filepath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(filepath);
Drawable drawable = new BitmapDrawable(Resources.getSystem(), bitmap);
imageView.setBackground(drawable);
}
break;
default:
break;
}
}
}
This is the MainActivity.java file. The code basically loads a image from gallery and displays it in imageview section of the layout.
When I ran the apk on my smartphone, no image was seen in imageview section after selection and after debugging it was seen that bitmap is equal to null.
Can someone please help me with this problem as I'm unable to solve it.

Make sure the obtained file path is a valid path.
Make sure you have the permissions to access storage.
If above things are okay and you still get null result, try following code:
Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

Related

Processing intent with camera image between two Activities and passing it to marker on Google Maps API does not work

I am new to Android and working on an Android app. The app has two activities: An Edit Activity and a Maps Activity.
In the Edit activity, the user can input data and take a picture from the Android phone, which works fine. The other Activity is a Maps Activity, where Markers on a map are displayed.
The markers on the map contain a bitmap, which also works fine. The part that isn´t working, is the part that the camera picture is placed on the marker instead of the bitmap graphic.
The image from the camera is displayed in an image view for the Edit Activity, which works fine.
The part that is not working is passing the camera intent to the other activity and placing the content of the intent on the marker.
Here´s the Edit Activity (a lot of code is commented out):
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static android.R.attr.data;
import static android.R.attr.id;
import static android.content.ContentValues.TAG;
import static android.provider.MediaStore.ACTION_IMAGE_CAPTURE;
public class EditActivity extends Activity {
public static int count = 0;
static final int REQUEST_TAKE_PHOTO = 1;
private static final String KEY_MARKER_ID = "id";
private static final String KEY_MARKER_TITLE = "title";
private static final String KEY_MARKER_DATE = "date";
private static final String KEY_MARKER_LOC = "location";
private static final String KEY_MARKER_LAT = "latlng";
private static final String KEY_MARKER_NAME = "name";
private static final String KEY_MARKER_PIC = "picture";
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editactivity);
PostsDatabaseHelper helper = PostsDatabaseHelper.getInstance(this);
helper.getReadableDatabase();
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.btnCapture);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
final LatLng latlng = getIntent().getParcelableExtra("location");
//final MarkerImage markerImage = getIntent().getParcelableExtra("data");
final EditText title = (EditText) findViewById(R.id.title);
final EditText date = (EditText) findViewById(R.id.date);
final EditText location = (EditText) findViewById(R.id.location);
final ImageView image = (ImageView) findViewById(R.id.imageView1);
final EditText name = (EditText) findViewById(R.id.name);
Button button = (Button) findViewById(R.id.save);
final Button camerabutton = (Button) findViewById(R.id.btnCapture);
final Intent getCameraImage = new Intent(ACTION_IMAGE_CAPTURE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
MarkerOptions marker = new MarkerOptions().position(latlng)
.icon(BitmapDescriptorFactory.fromBitmap(bmp)
);
if (title.getText() != null) {
marker.title(title.getText().toString());
}
if (date.getText() != null) {
marker.getPosition();
Log.d("String Value of Marker:", String.valueOf(marker));
}
Intent resultIntent = new Intent();
resultIntent.putExtra("marker", marker);
resultIntent.putExtra("picture", getCameraImage);
Log.d("Längen-und Breitengrad:", String.valueOf(marker));
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
// 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();
//final Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
/*if (getCameraImage.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
SQLiteDatabase db = helper.getWritableDatabase();
db.beginTransaction();
try {
// The user might already exist in the database (i.e. the same user created multiple posts).
//long markerId = addOrUpdateMarker(marker.user);
ContentValues values = new ContentValues();
values.put(KEY_MARKER_ID, id);
values.put(KEY_MARKER_TITLE, String.valueOf(title));
values.put(KEY_MARKER_NAME, String.valueOf(name));
values.put(KEY_MARKER_LOC, String.valueOf(location));
values.put(KEY_MARKER_DATE, String.valueOf(date));
// Notice how we haven't specified the primary key. SQLite auto increments the primary key column.
db.insertOrThrow(TABLE_MARKERS, null, values);
db.setTransactionSuccessful();
} catch (Exception e) {
Log.d(TAG, "Error while trying to add post to database");
} finally {
db.endTransaction();
}
/*File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
photoFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(cameraIntent, REQUEST_TAKE_PHOTO);
}
}
/*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);
//cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, REQUEST_TAKE_PHOTO);
}
});
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
// ".jpg", /* suffix */
// storageDir /* directory */
//);
// Save a file: path for use with ACTION_VIEW intents
//mCurrentPhotoPath = image.getAbsolutePath();
//return image;
}
/*#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
}
}
/*if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) {
Log.d("CameraDemo", "Pic saved");
}*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
Here´s the Maps Activity:
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import gmbh.webagenten.recycling.PostsDatabaseHelper;
import gmbh.webagenten.recycling.SQLiteSampleActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import static android.content.ContentValues.TAG;
import static gmbh.webagenten.recycling.R.attr.title;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private static final int EDIT_REQUEST = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
// Get singleton instance of database
//PostsDatabaseHelper databaseHelper = PostsDatabaseHelper.getInstance((Context) this);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.id.imageView1);
this.mMap = map;
try {
mMap.setMyLocationEnabled(true);
} catch (SecurityException e) {
LatLng hamburg = new LatLng(53.551085, 9.993682);
mMap.addMarker(new MarkerOptions().position(hamburg).title("Marker in Hamburg")
.icon(BitmapDescriptorFactory.fromBitmap(bmp)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(hamburg));
//PostsDatabaseHelper helper = PostsDatabaseHelper.getInstance(this);
//helper.getReadableDatabase();
//Log.d(TAG, "Database in use");
}
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(final LatLng latLng) {
Intent edit = new Intent(MapsActivity.this, EditActivity.class);
edit.putExtra("location", latLng);
Log.d("Längen-und Breitengrad:", String.valueOf(latLng));
MapsActivity.this.startActivityForResult(edit, EDIT_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (EDIT_REQUEST) : {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
MarkerOptions markerOptions = data.getParcelableExtra("marker");
MarkerOptions markerImage = data.getParcelableExtra("data");
mMap.addMarker(markerOptions);
//mMap.addMarker(markerImage);
}
break;
}
}
}
}
/**
* Manipulates the map once available.
* This callback is triggered when the map is ready to be used.
* This is where we can add markers or lines, add listeners or move the camera. In this case,
* we just add a marker near Sydney, Australia.
* If Google Play services is not installed on the device, the user will be prompted to install
* it inside the SupportMapFragment. This method will only be triggered once the user has
* installed Google Play services and returned to the app.
*/
/*
#Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
try {
mMap.setMyLocationEnabled(true);
} catch (SecurityException e) {
LatLng hamburg = new LatLng(53.551085, 9.993682);
mMap.addMarker(new MarkerOptions().position(hamburg).title("Marker in Hamburg"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(hamburg));
mMap.setOnMapClickListener(this);
}
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(final LatLng latLng) {
Intent edit = new Intent(MapsActivity.this, EditActivity.class);
edit.putExtra("location", latLng);
MapsActivity.this.startActivityForResult(edit, EDIT_REQUEST);
}
});
}
*/
The XML files:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#222"
android:id="#+id/llMarker">
<ImageView
android:id="#+id/ivMarker"
android:layout_marginTop="5dp"
android:layout_width="40dp"
android:layout_height="40dp" />
<item>
<ImageView android:id="#+id/imageView1" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
</item>
</LinearLayout>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title"
android:inputType="text" />
<EditText
android:id="#+id/date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Date"
android:inputType="date" />
<EditText
android:id="#+id/location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Location"
android:inputType="text" />
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="text" />
<Button
android:id="#+id/btnCapture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Camera" />
<Button
android:id="#+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save"/>
<ImageView android:id="#+id/imageView1" android:layout_height="wrap_content" android:layout_width="wrap_content"></ImageView>
As I am new to Android, I am stuck with this problem for a longer time now. Any help or hints would be appreciated, thanks!
I understand your problem . What I got the mistake is , you are not able to get the bitmap to the next activity , you have set the data to the imageview in edit activity and when you are opening the map activity, that data is lost ,
what you have to do is:
1. pass the bitmap to the map activity from edit activity
2. get the bitmap to the map activity and then show the bitmap to the map.
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v4.content.FileProvider;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import static android.R.attr.data;
import static android.R.attr.id;
import static android.content.ContentValues.TAG;
import static android.provider.MediaStore.ACTION_IMAGE_CAPTURE;
public class EditActivity extends Activity {
public static int count = 0;
static final int REQUEST_TAKE_PHOTO = 1;
private static final String KEY_MARKER_ID = "id";
private static final String KEY_MARKER_TITLE = "title";
private static final String KEY_MARKER_DATE = "date";
private static final String KEY_MARKER_LOC = "location";
private static final String KEY_MARKER_LAT = "latlng";
private static final String KEY_MARKER_NAME = "name";
private static final String KEY_MARKER_PIC = "picture";
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
Bitmap photo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.editactivity);
PostsDatabaseHelper helper = PostsDatabaseHelper.getInstance(this);
helper.getReadableDatabase();
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.btnCapture);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
final LatLng latlng = getIntent().getParcelableExtra("location");
//final MarkerImage markerImage = getIntent().getParcelableExtra("data");
final EditText title = (EditText) findViewById(R.id.title);
final EditText date = (EditText) findViewById(R.id.date);
final EditText location = (EditText) findViewById(R.id.location);
final ImageView image = (ImageView) findViewById(R.id.imageView1);
final EditText name = (EditText) findViewById(R.id.name);
Button button = (Button) findViewById(R.id.save);
final Button camerabutton = (Button) findViewById(R.id.btnCapture);
final Intent getCameraImage = new Intent(ACTION_IMAGE_CAPTURE);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(final View view) {
Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
MarkerOptions marker = new MarkerOptions().position(latlng)
.icon(BitmapDescriptorFactory.fromBitmap(bmp)
);
if (title.getText() != null) {
marker.title(title.getText().toString());
}
if (date.getText() != null) {
marker.getPosition();
Log.d("String Value of Marker:", String.valueOf(marker));
}
Intent resultIntent = new Intent();
resultIntent.putExtra("marker", marker);
resultIntent.putExtra("picture", getCameraImage);
intent.putExtra("BitmapImage", photo); // passing the bitmap to the next activity . and retrieve it to the next activity
Log.d("Längen-und Breitengrad:", String.valueOf(marker));
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
`photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
Here is the map activity
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import gmbh.webagenten.recycling.PostsDatabaseHelper;
import gmbh.webagenten.recycling.SQLiteSampleActivity;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import static android.content.ContentValues.TAG;
import static gmbh.webagenten.recycling.R.attr.title;
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback {
private GoogleMap mMap;
private static final int EDIT_REQUEST = 1;
Bitmap bmp;
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
// Get singleton instance of database
Intent intent = getIntent();
bmp = (Bitmap) intent.getParcelableExtra("BitmapImage"); // getting the bitmap data from the edit activity.
//PostsDatabaseHelper databaseHelper = PostsDatabaseHelper.getInstance((Context) this);
setContentView(R.layout.activity_maps);
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
#Override
public void onMapReady(GoogleMap map) {
//Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.id.imageView1);
this.mMap = map;
try {
mMap.setMyLocationEnabled(true);
} catch (SecurityException e) {
LatLng hamburg = new LatLng(53.551085, 9.993682);
mMap.addMarker(new MarkerOptions().position(hamburg).title("Marker in Hamburg")
.icon(BitmapDescriptorFactory.fromBitmap(bmp)));
mMap.moveCamera(CameraUpdateFactory.newLatLng(hamburg));
//PostsDatabaseHelper helper = PostsDatabaseHelper.getInstance(this);
//helper.getReadableDatabase();
//Log.d(TAG, "Database in use");
}
mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {
#Override
public void onMapClick(final LatLng latLng) {
Intent edit = new Intent(MapsActivity.this, EditActivity.class);
edit.putExtra("location", latLng);
Log.d("Längen-und Breitengrad:", String.valueOf(latLng));
MapsActivity.this.startActivityForResult(edit, EDIT_REQUEST);
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case (EDIT_REQUEST) : {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getExtras();
MarkerOptions markerOptions = data.getParcelableExtra("marker");
MarkerOptions markerImage = data.getParcelableExtra("data");
mMap.addMarker(markerOptions);
//mMap.addMarker(markerImage);
}
break;
}
}
}
}
Here is the updated code, Hope it help .Kindly check it and let me know.

The method createBitmapFromURI(uri) in the type ShowPhoto is not applicable for the arguments(string)

I am making an app which can make and save a photo, and I want to display the photo which is made, im this class below. But I am getting the error: "The method createBitmapFromURI(uri) in the type ShowPhoto is not applicable for the arguments(string)". Any idea for I can fix this?
ShowPhoto.java:
package com.example.photoviewer;
import java.io.FileNotFoundException;
import com.example.keyfinder.R;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class ShowPhoto extends Activity{
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.watch_pic);
iv = (ImageView) findViewById(R.id.imageView1);
String uriOfPicture = getIntent().getStringExtra("uri");
Bitmap image=createBitmapFromURI(uriOfPicture); //error here
iv.setImageBitmap(image);
}
public Bitmap createBitmapFromURI(Uri uriOfPicture) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getApplicationContext()
.getContentResolver().openInputStream(uriOfPicture));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (bitmap != null)
return Bitmap.createScaledBitmap(bitmap, 100, 100, true);
else {
return null;
}
}
}
EDIT: Another error occured meanwhile in my PhotoMaker.java class
This is the logcat: http://imgur.com/3K5loY0
This is the Photomaker.java class:
package com.example.photoviewer;
import java.io.FileNotFoundException;
import com.example.keyfinder.R;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.net.Uri;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class FotoMaker extends Activity
{
ImageView iv;
Uri uriOfPicture;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.add_pic);
iv = (ImageView) findViewById(R.id.imageView);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick (View v){
takeImageFromCamera();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 0)
{
Bitmap image=createBitmapFromURI(uriOfPicture);
iv.setImageBitmap(image);
}
Intent intent = new Intent(this,ShowPhoto.class);
intent.putExtra("uri", uriOfPicture);
startActivity(intent);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (uriOfPicture != null) {
outState.putString("cameraImageUri", uriOfPicture.toString());
}
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState.containsKey("cameraImageUri")) {
uriOfPicture = Uri.parse(savedInstanceState.getString("cameraImageUri"));
}
}
/*********************************** method to take URI of the picture *******************/
public void takeImageFromCamera(){
String fileName = "new-photo-name.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image captured by camera");
uriOfPicture = getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
Log.i("uri",uriOfPicture.toString());
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriOfPicture);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent,
0);
}
public Bitmap createBitmapFromURI(Uri uriOfPicture) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getApplicationContext()
.getContentResolver().openInputStream(uriOfPicture));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (bitmap != null)
return Bitmap.createScaledBitmap(bitmap, 100, 100, true);
else {
return null;
}
}
}
Get Uri from String using Uri parse function :
Bitmap image=createBitmapFromURI(Uri.parse(uriOfPicture));

How to show a picture in a class, which I saved in another class

I got an application where you can make and save a picture. Now I when this picture to display in another class. I just started programming in java and have no idea how to do this.
FotoMaker.java:
package com.example.random;
import java.io.FileNotFoundException;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.net.Uri;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class FotoMaker extends Activity
{
ImageView iv;
Uri uriOfPicture;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.addpictolist);
iv = (ImageView) findViewById(R.id.imageView);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick (View v){
takeImageFromCamera();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(requestCode == 0)
{
Bitmap image=createBitmapFromURI(uriOfPicture);
iv.setImageBitmap(image);
}
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (uriOfPicture != null) {
outState.putString("cameraImageUri", uriOfPicture.toString());
}
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState.containsKey("cameraImageUri")) {
uriOfPicture = Uri.parse(savedInstanceState.getString("cameraImageUri"));
}
}
public void takeImageFromCamera(){
String fileName = "new-photo-name.jpg";
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,
"Image captured by camera");
uriOfPicture = getContentResolver()
.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
values);
Log.i("uri",uriOfPicture.toString());
Intent intent = new Intent(
MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uriOfPicture);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent,
0);
}
public Bitmap createBitmapFromURI(Uri uri) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getApplicationContext()
.getContentResolver().openInputStream(uri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (bitmap != null)
return Bitmap.createScaledBitmap(bitmap, 100, 100, true);
else {
return null;
}
}
}
ShowPhoto.java:
package com.example.random;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class ShowPhoto extends Activity{
ImageView iv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.watch_pic);
iv = (ImageView) findViewById(R.id.imageView1);
}
}
You can pass the URI to the new activity once you have it. So, in your FotoMaker's onActivityResult just start the ShowPhoto activity and send the uriOfPicture in an extra.
Intent intent = new Intent(this, ShowPhoto.class);
intent.putExtra("uri", uriOfPicture);
startActivity(intent);
In your ShowPhoto activity just catch the argument and show it:
String uriOfPicture = getIntent().getStringExtra("uri");
Bitmap image = createBitmapFromURI(uriOfPicture);
iv.setImageBitmap(image);
I'm assuming most of your code already works.

Unable to save images taken using app?

I have spent much time trying to resolve this trivial problem but seem to be going in circles even after reading many tutorials and previously asked questions.
I am a novice at programming but want to get better and believe that learning through mistakes is the best way.
Can someone please help identify the problem with this code and what can be done in order to allow the pictures taken by the device's camera to be saved onto the phone afterwards rather than being discarded and previewed.
Thanks in advance!
Java code for relevant class:
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
public class Activity_Camera extends Activity implements View.OnClickListener {
public static final int cameraData = 1;
ImageButton ib;
ImageView iv;
Intent i;
Bitmap bmp;
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.Info:
Intent i = new Intent(this, Help.class);
startActivity(i);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
initialise();
}
private void initialise() {
iv = (ImageView) findViewById(R.id.ivPicReturn);
ib = (ImageButton) findViewById(R.id.ibTakePic);
ib.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
//
i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
//
startActivityForResult(i, cameraData);
break;
}
}
//
private String getOutputMediaFileUri() {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image.jpg");
Uri uriSavedImage = Uri.fromFile(image);
Intent imageIntent = null;
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
return null;
}
//
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
bmp = (Bitmap) extras.get("data");
iv.setImageBitmap(bmp);
}
}
}
because you're returning null return null; in your getOutputMediaFileUri
try this instead
public void onClick(View v) {
switch (v.getId()) {
case R.id.ibTakePic:
i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, getOutputMediaFileUri());
startActivityForResult(i, cameraData);
break;
}
}
private Uri getOutputMediaFileUri() {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image.jpg");
return Uri.fromFile(image);
}

Issues With the OCR code and Java Code for ANDROID

here is my code to convert the picture taken from the camera into text using tesseract's java wrapper tess4j! i have included the jar files added them to my path! code does not show any syntax errors! i launch the default camera it starts i take picture and it is shown in the image view i declared however the editbox seems empty though it should show the result of OCR!
package your.apnakaam.namespace;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.graphics.Bitmap;
import android.widget.ImageView;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.util.Log;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.io.File;
import java.io.File;
import net.sourceforge.tess4j.*;
public class KaamsekhaActivity extends Activity
{
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
//**************************************************************************************************
private Intent data;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setContentView(R.layout.apna_layout);
Button capt_but = (Button)findViewById(R.id.capture_btn);
this.imageView = (ImageView)this.findViewById(R.id.picture);
capt_but.setOnClickListener(new View.OnClickListener()
{
//#Override
// TODO Auto-generated method stub
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
{
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
void myfunction()
}
}
public void myfunction()
{
TextView disp = (TextView)findViewById(R.id.editText1);
File new_pic = (File) data.getExtras().get("data");
Tesseract instance = Tesseract.getInstance(); // JNA Interface Mapping
try
{
String result = instance.doOCR(new_pic);
disp.setText(result);
}
catch (TesseractException e)
{
System.err.println(e.getMessage());
}
}
}
I can't figure out whats wrong with the code!
The function doOCR takes a buffered image that is not possible while working in android! Bufferedimage uses IIOimage that is used in java but not in android!

Categories