hey i want to share my image in imageview to instagram and i using ACTION_SEND
before i want to share image i get my picture from other activity
i run app and i getting message "unable to download file"
and this is my code .... check my code if any error
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.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
public class editPhoto extends Activity {
String picturePath;
#Override
protected void onCreate(Bundle savedInstanceState) {
overridePendingTransition(R.anim.push_left_in, R.anim.hold);
super.onCreate(savedInstanceState);
setContentView(R.layout.page);
picturePath = getIntent().getStringExtra("selectedPhoto");
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
ImageView imageView = (ImageView) findViewById(R.id.iv_pic);
imageView.setImageBitmap(bitmap);
TextView tv = (TextView) findViewById(R.id.imagepath);
tv.setText(picturePath);
Button share = (Button) findViewById(R.id.btnShare);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(picturePath));
startActivity(Intent.createChooser(intent, "Share"));
}
});
}
}
put in onClickListener
imageView.setDrawingCacheEnabled(true);
Bitmap bitmap = imageView.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try {
cachePath.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.close();
} catch (Exception e) {
e.printStackTrace();
}
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
startActivity(Intent.createChooser(share,"Share via"));
Related
I used the Picasso library to load image in PhotoView, but I am not able to get the imageUri when using getLocalBitmapUri.
package com.shahryarsoftwares.chatbook;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.github.chrisbanes.photoview.PhotoView;
import com.github.chrisbanes.photoview.PhotoViewAttacher;
import com.squareup.picasso.Picasso;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
public class ImageViewerActivity extends AppCompatActivity {
private Toolbar mToolbar;
private PhotoView photoView;
private PhotoViewAttacher mAttacher;
private Button shareBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_viewer);
Bundle extras = getIntent().getExtras();
String user_name = extras.getString("user_name");
final String user_image = extras.getString("user_image");
mToolbar = (Toolbar) findViewById(R.id.image_viewer_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle(user_name);
photoView = (PhotoView) findViewById(R.id.photo_view);
Picasso.with(ImageViewerActivity.this).load(user_image).placeholder(R.drawable.default_profile).into(photoView);
mAttacher = new PhotoViewAttacher(photoView);
shareBtn = (Button) findViewById(R.id.image_viewer_share_btn);
shareBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Uri bmpUri = getLocalBitmapUri(photoView);
if(bmpUri != null){
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share Image"));
}else{
Toast.makeText(ImageViewerActivity.this, "No image selected", Toast.LENGTH_SHORT).show();
}
}
});
//mAttacher.update();
}
public Uri getLocalBitmapUri(PhotoView photoView) {
// Extract Bitmap from ImageView drawable
Drawable drawable = photoView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable){
bmp = ((BitmapDrawable) photoView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS), "share_image_" + System.currentTimeMillis() + ".jpg");
file.getParentFile().mkdirs();
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
}
Where am I making a mistake? I am taking my input image URI from another activity and using putExtra() and get that activity by using getExtra().
i have also used the below code:
shareBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Target shareTarget = new Target() {
#Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Uri bmpUri = getLocalBitmapUri(photoView);
if(bmpUri != null){
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share Image"));
}else{
Toast.makeText(ImageViewerActivity.this, "No image selected", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onBitmapFailed(Drawable errorDrawable) {
}
#Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
};
Picasso.with(ImageViewerActivity.this).load(user_image).placeholder(R.drawable.default_profile).into(shareTarget);
startShare(user_image);
}
});
But also doing this, it didn't get resolved.
I want to send images via email in my android app. For which I'm using Android Native Camera app and Intents to use the respective service. I've used the following code:
Email is getting send but if I'm trying to add image the app gets crash.
public class Complaints extends AppCompatActivity {
Button sendEmail;
EditText to, subject, msg;
Bitmap image;
Button camera;
File pic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complaints);
to = (EditText) findViewById(R.id.et1);
subject = (EditText) findViewById(R.id.et2);
msg = (EditText) findViewById(R.id.et3);
sendEmail = (Button) findViewById(R.id.s_Email);
camera = (Button) findViewById(R.id.btn_img);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent=new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(Intent.createChooser(intent,"Select Picture"));
}
});
sendEmail.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String Emailid = to.getText().toString();
String sub = subject.getText().toString();
String message = msg.getText().toString();
Intent email = new Intent(Intent.ACTION_SEND);
email.putExtra(Intent.EXTRA_EMAIL, new String[]{Emailid});
email.putExtra(Intent.EXTRA_SUBJECT, sub);
email.putExtra(Intent.EXTRA_TEXT, message);
email.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);//this will make such that when user returns to your app, your app is displayed, instead of the email app.
email.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
email.setType("message/rfc822");
email.setType("image/jpeg");
try {
startActivity(Intent.createChooser(email, "Message was Sent"));
}
catch (ActivityNotFoundException e) {
Toast t = Toast.makeText(Complaints.this, "There is No Emial Client installed ", Toast.LENGTH_SHORT);
t.setGravity(Gravity.CENTER, 0, 10);
t.show();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10) {
image = (Bitmap) data.getExtras().get("Data");
ImageView i = (ImageView) findViewById(R.id.img);
i.setImageBitmap(image);
try
{
File root= Environment.getExternalStorageDirectory();
if(root.canWrite())
{
pic=new File(root,"pic.jpeg");
FileOutputStream out=new FileOutputStream(pic);
image.compress(Bitmap.CompressFormat.JPEG,100,out);
out.flush();
out.close();
}
} catch (IOException e)
{
Log.e("BROKEN", "Could not write file " + e.getMessage());
}
}
}
}
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import android.content.ContentValues;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.net.Uri;
import android.provider.MediaStore.Images;
import android.provider.MediaStore.Images.Media;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
public class Complaints extends Activity {
Button send;
Bitmap thumbnail;
File pic;
EditText address, subject, emailtext;
protected static final int CAMERA_PIC_REQUEST = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complaints);
send=(Button) findViewById(R.id.emailsendbutton);
address=(EditText) findViewById(R.id.emailaddress);
subject=(EditText) findViewById(R.id.emailsubject);
emailtext=(EditText) findViewById(R.id.emailtext);
Button camera = (Button) findViewById(R.id.button1);
camera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
});
send.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, new String[]{"dummy#email.com"});
i.putExtra(Intent.EXTRA_SUBJECT,"dummy subject");
//Log.d("URI#!##!#!###!", Uri.fromFile(pic).toString() + " " + pic.exists());
i.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(pic));
i.setType("image/png");
startActivity(Intent.createChooser(i,"Share this"));
}
});
}
Getting The Image Path and converting path into Uri :
File photo = new File(Environment.getExternalStorageDirectory()+"/Android/data/"+getApplicationContext().getPackageName()+"/Fault", imagename+".png")
Uri imageuri = Uri.fromFile(photo);
Sending through Email Intent :
Intent send_report = new Intent(Intent.ACTION_SEND);
send_report.putExtra(Intent.EXTRA_EMAIL, new String[]{ email_emailid});
send_report.putExtra(Intent.EXTRA_SUBJECT, email_subject);
send_report.putExtra(Intent.EXTRA_STREAM, imageuri);
send_report.putExtra(Intent.EXTRA_TEXT, email_body);
send_report.setType("text/plain");
send_report.setType("image/png");
startActivityForResult(Intent.createChooser(send_report, "Choose an Email client"), 77);
I am using the following code for clicking a photo on Button click.
package com.example.clickpic;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
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.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count=0;
private ImageView imageView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//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();
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button capture = (Button) findViewById(R.id.btnCapture);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// here,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);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1888);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1888 && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
The code works perfectly well but it just opens up the camera activity. What must I do so that on button click, it clicks a picture and saves it as well?
Right now, this just opens the camera app of the phone, and expects me to click the picture and click on OK. I don't want these things.
All I want is, when I click on the button from my app it should click a picture without expecting any additional input from me.
Edit
I have already tried to create a camera app of my own, but I ran into some issues.
That's why I am trying this approach. Camera API not working on KITKAT
All I want is, when I click on the button from my app it should click a picture without expecting any additional input from me. -
Its not achieved by Camera Intent like android.provider.MediaStore.ACTION_IMAGE_CAPTURE. You have to implement camera interface portion in your code without using native camera application of Device.
Look at portion of Building Camera App
I have written a camera class which takes picture, arranges the orientation (some devices takes photo horizontal as default) and saves the photo taken. You can check it from the link below:
Camera capture orientation on samsung devices in android
Edit: Sorry, savePhoto functions are not written in my example. Adding them now.
savePhoto function:
public void savePhoto(Bitmap bmp) {
imageFileFolder = new File(Environment.getExternalStorageDirectory(),
cc.getDirectoryName());
imageFileFolder.mkdir();
FileOutputStream out = null;
Calendar c = Calendar.getInstance();
String date = fromInt(c.get(Calendar.MONTH))
+ fromInt(c.get(Calendar.DAY_OF_MONTH))
+ fromInt(c.get(Calendar.YEAR))
+ fromInt(c.get(Calendar.HOUR_OF_DAY))
+ fromInt(c.get(Calendar.MINUTE))
+ fromInt(c.get(Calendar.SECOND));
imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
try {
out = new FileOutputStream(imageFileName);
bmp.compress(Bitmap.CompressFormat.JPEG, 70, out);
out.flush();
out.close();
scanPhoto(imageFileName.toString());
out = null;
} catch (Exception e) {
e.printStackTrace();
}
}
scanPhoto function:
public void scanPhoto(final String imageFileName) {
geniusPath = imageFileName;
msConn = new MediaScannerConnection(MyClass.this,
new MediaScannerConnectionClient() {
public void onMediaScannerConnected() {
msConn.scanFile(imageFileName, null);
}
#Override
public void onScanCompleted(String path, Uri uri) {
msConn.disconnect();
}
});
msConn.connect();
}
SavePhotoTask class:
class SavePhotoTask extends AsyncTask<byte[], String, String> {
#Override
protected String doInBackground(byte[]... jpeg) {
File photo = new File(Environment.getExternalStorageDirectory(),
"photo.jpg");
if (photo.exists()) {
photo.delete();
}
try {
FileOutputStream fos = new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
} catch (java.io.IOException e) {
}
return (null);
}
}
Hey I am new to android and eclipse environment.I've no idea of Java.I'm trying to create an app to open the camera of an android device.This is how my main activity.java looks like
package com.example.trycamera2;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button_send);*****
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xray");
dir.mkdirs();
File file = new File(dir, "tmp_avatar_"
+ String.valueOf(System.currentTimeMillis()) + ".jpg");
fileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
});
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Bitmap tempBitmap = (Bitmap) data.getExtras().get("data");
FileOutputStream out;
try {
out = new FileOutputStream(fileUri.getPath());
tempBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
Log.v("ManageImage-other", "another phone type");
e.printStackTrace();
}
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
}
There is a red mark under button_send in the star marked line...it shows an alternative action_settings instead of button_send....but when I replace with action_settings and run the launch is cancelled.Failed to install .apk on emulator..any help will be of great use.
Sounds like Android can't find your button in any of the layouts. Double check your layouts to make sure they're there.
If they are, just go to the Project menu at the top, and select Clean. Select your project, then click Okay. It will take moment to clean and build, but that might fix your issue.
I have the following code that pulls a pdf document from a remote viewer to view:
package com.example.techvault;
import java.io.File;
import java.io.IOException;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
public class PDFFromServerActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_document);
Intent in= getIntent(); // gets the previously created intent
String url = in.getStringExtra("full_url");
if (!url.startsWith("http://") && !url.startsWith("https://")){
url = "http://ezdrawdocs.com" + url;
}
Log.d("pdf url: ",url);
String extStorageDirectory = Environment.getExternalStorageDirectory()
.toString();
File folder = new File(extStorageDirectory, "pdf");
folder.mkdir();
File file = new File(folder, "Read.pdf");
try {
file.createNewFile();
} catch (IOException e1) {
e1.printStackTrace();
}
Downloader.DownloadFile(url, file);
showPdf();
}
public void showPdf()
{
File file = new File(Environment.getExternalStorageDirectory()+"/pdf/Read.pdf");
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);
}
}
When the backarrow is selected it redirects to a blank screen. How can I control which activity to redirect to when backarrow is selected?
Thanks
You can define that in your manifest under the activity with
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.yourapp.SomeActivity" />
What you're doing here is implementing what I call a "Passthrough Activity" (an Activity that has no UI and immediately launches a different Activity)... I personally try to avoid them.
That said, what you need to do is call Activity.finish() after showPDF() in Activity.onCreate(). That will close your passthrough so that when you back out of the PDF viewer you will not come back to the passthrough.