camera captured image share to different application - android

I am working on a application in which there a part of taking images
from camera and have to share images to the different applications . I am using a code but it is not sharing the images. please have a look and please tell where I am wrong.
image = (ImageView)findViewById(R.id.image);
share = (Button)findViewById(R.id.share);
click = (Button)findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BitmapDrawable bitmapDrawable = (BitmapDrawable)image.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
// Save this bitmap to a file.
File cache = getApplicationContext().getExternalCacheDir();
File sharefile = new File(cache, "toshare.png");
try {
FileOutputStream out = new FileOutputStream(sharefile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (IOException e) {
}
// Now send it out to share
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/*");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + sharefile));
try {
startActivity(Intent.createChooser(share, "Share photo"));
} catch (Exception e) {
}
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
//2
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
image.setImageBitmap(thumbnail);
//3
share.setVisibility(0);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
//4
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "image.jpg");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
//5
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}

Related

Put string url data to bitmap [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I'm making a wallpaper application. In this full-screen image activity, this activity is getting data from previous activity by intent. Now I want to set the image(that comes from URL) as wallpaper. This code is not working.
public class PhotoFullPopupWindow extends AppCompatActivity {
Activity context;
Bitmap bitmap=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_screen_image_view);
ImageView fullScreenImageView = findViewById(R.id.FullScreenImageView);
final String url=getIntent().getStringExtra("url");
Glide.with(this)
.load(url)
.into(fullScreenImageView);
context=this;
bitmap = getBitmap(url);
Button setWallpaperButton = findViewById(R.id.setWallpaper);
setWallpaperButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException ex) {
ex.printStackTrace();
}
}
});
}
public Bitmap getBitmap(String bitmapUrl) {
try {
URL URL = new URL(bitmapUrl);
return BitmapFactory.decodeStream(URL.openConnection().getInputStream());
}
catch(Exception ex) {
return null;
}
}
}
below is the code i used for setting the wallpaper
public void setWallpaper(String url) {
WallpaperManager myWallManager = WallpaperManager.getInstance(getApplicationContext());
Glide.with(this)
.asBitmap()
.load(url)
.into(new SimpleTarget<Bitmap>() {
#Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
Intent intent = new Intent(Intent.ACTION_ATTACH_DATA);
intent.setDataAndType(getLocalBitmapUri(resource), "image/*");
intent.putExtra("jpg", "image/*");
startActivity(Intent.createChooser(
intent, "Set as:"));
}
private Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
try {
File file = new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),
"OP_Wallpaper_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
Try this:
In your first activity, you should save the Bitmap to disk.
Then load it up in the next activity.
Make sure to recycle your bitmap in the first activity to prime it for garbage collection.
In Activity 1:
try {
//Write file
String filename = "bitmap.png";
FileOutputStream stream = this.openFileOutput(filename, Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
//Cleanup
stream.close();
bmp.recycle();
//Pop intent
Intent in1 = new Intent(this, Activity2.class);
in1.putExtra("image", filename);
startActivity(in1);
} catch (Exception e) {
e.printStackTrace();
}
In Activity 2:
Bitmap bmp = null;
String filename = getIntent().getStringExtra("image");
try {
FileInputStream is = this.openFileInput(filename);
bmp = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
}

Image not showing when passed as an intent extra

I'm trying to pass the screenshot of screen with an explicit intent but the screen shows black screenshot(refer image here). As soon as i click share, a toast appears saying sending failed. Here's the code to capture screenshot and send it to other app:
public void getScreenShot(View view) {
View screenView = view.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
f = new File(this.getFilesDir(), "screenshotFile");
try {
if (!f.exists())
f.createNewFile();
} catch (IOexception e) {
e.printStackTrace();
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 10, bos);
byte[] bitmapdata = bos.toByteArray();
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
This code sends the data to whatsapp:
public void shareWhatsapp(View view) {
try {
myVib.vibrate(50);
getScreenShot(view);
//String fileName = "screenshotFile";
//Bitmap bitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
try {
intent.setPackage("com.whatsapp");
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(MainActivity.this, "App not installed", Toast.LENGTH_SHORT).show();
}
//TODO: APP CAN CRASH HERE
if (position > 0) {
try {
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f);
} catch (Exception e) {
e.printStackTrace();
} finally {
intent.putExtra(Intent.EXTRA_TEXT, Titles.get(position - 1) + ": " + Links.get(position - 1)); //position problems
}
} else {
try {
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f);
} catch (Exception e) {
e.printStackTrace();
} finally {
intent.putExtra(Intent.EXTRA_TEXT, Titles.get(0) + ": " + Links.get(0)); //position problems
}
}
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
Can someone help me with this?
First, you are writing the file to internal storage. Third-party apps do not have access to your app's internal storage.
Second, you are using Uri.fromFile(), which is starting to be discontinued.
Your safest long-term course of action is to have a ContentProvider serve your file from its location on internal storage, then use a Uri associated with that ContentProvider.

How to share a image from its url

I am developing an android application and i need to share the image on button click.But i am getting Image URl only. So, how can i share the image???
And i am getting empty attachment if i give image URL to the intent.
my code is:
sharebut =(Button)findViewById(R.id.sharebut);
sharebut.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
String screenshotUri = flag;
sharingIntent.setType("image/*");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using"));
}
});
Add the path where your image is located in sd card in Uri.parse("file:///"+ yourImagePath)
Use :
String path= "/Downloads/image1.jpg"; //Add your path here
sharingIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file:///"+ path));
Please try this solution for share image via email from URL.
String path = "";
URL url;
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setClassName("com.google.android.gm",
"com.google.android.gm.ComposeActivityGmail");
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, description);
try {
url = new URL(thumnbnailURL);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap immutableBpm = BitmapFactory.decodeStream(input);
Bitmap mutableBitmap = immutableBpm.copy(
Bitmap.Config.ARGB_8888, true);
View view = new View(VideoDetailsActivity.this);
view.draw(new Canvas(mutableBitmap));
path = Images.Media.insertImage(getContentResolver(),
mutableBitmap, "Nur", null);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Uri uri = Uri.parse(path);
intent.setType("application/image");
intent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(intent);
I found the solution.. :)
Just created a file and share the content in imageview.
sharebut =(Button)findViewById(R.id.sharebut);
sharebut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
imgflag.buildDrawingCache();
Bitmap bmap = imgflag.getDrawingCache();
OutputStream out = null;
String path =Environment.getExternalStorageDirectory().toString();
File file = new File(path, "test.png");
try {
file.createNewFile();
out = new FileOutputStream(file);
bmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share Your Image"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});

Upload photo from gallery or take from camera in Webview

My application is webbased and need to upload photos, website have a file input button, i made it work with this
wv = new WebView(this);
wv.setWebViewClient(new WebViewClient());
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setAllowFileAccess(true);
wv.setWebChromeClient(new WebChromeClient()
{
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult( Intent.createChooser( i, "File Chooser" ), MainActivity.FILECHOOSER_RESULTCODE );
}
but it shows just gallery to pick photos, i need to take from camera at the same time.
i tried this solution Upload camera photo and filechooser from webview INPUT field but its only opening camera, not uploading taken photo
In your example
wv.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView v, String url) {
if (url.startsWith("testzapp:")) {
//do whatever action you had intended
}
}
}
here is my solution
if (url.startsWith("xxx")) {
String[] falid = url.split(":");
falidi = Integer.parseInt(falid[1]);
Intent intent = new Intent(
"android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, TAKE_PICTURE);
return true;
}
public void onActivityResult(int requestCode, int resultCode,
final Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == TAKE_PICTURE) {
try {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
Random randomGenerator = new Random();
randomGenerator.nextInt();
newimagename = falidi + "_" + randomGenerator.nextInt()
+ ".jpg";
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + newimagename);
try {
f.createNewFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// write the bytes in file
FileOutputStream fo = null;
try {
fo = new FileOutputStream(f.getAbsoluteFile());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fo.write(bytes.toByteArray());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String uri = f.getAbsolutePath();
// this is the url that where you are saved the
// image
File fx = new File(uri);
Bitmap bitmap = BitmapFactory.decodeFile(fx.getPath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 30, stream);
byte[] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr,
Base64.DEFAULT);
client = new DefaultHttpClient();
HttpPost post = new HttpPost(
"http://www.xxx.com/android/library/image.php?name="
+ newimagename);
List<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("image", image_str));
try {
post.setEntity(new UrlEncodedFormEntity(pairs));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Candemir
new ResultTask().execute(new HttpPost[] { post });
// Candemir
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

ImageCapture uri

I am trying to take the output of the captured image into a uri but it throws null pointer exception on another component which was not null before and when not storing the image in uri it runs okay
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri1 = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
"Photo" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri1);
cameraIntent.putExtra("return-data", true);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
and on activity result
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap temp=null;
Bundle extras = data.getExtras();
/*Uri g=data.getData();
try {
temp = MediaStore.Images.Media.getBitmap(getContentResolver(), g);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
*/
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
int h=photo.getHeight();
int w=photo.getWidth();
/* try {
temp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), mImageCaptureUri1);
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}*/
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
bitmapdata = stream.toByteArray();
Log.i("image", ""+bitmapdata);
image_boolean=true;
stream.flush();
stream.close();
/*File f = new File(mImageCaptureUri1.getPath());
if (f.exists()) f.delete();*/
}catch(Exception e)
{}
imageView.setImageBitmap(photo);
}}
}

Categories

Resources