Hi guys I am totally new in android app development and need some helped. Actually I am working on project i.e. WhatsApp Status Saver.
I did some basic coding.
But Now I want to share video from VideoView.
I done the code for sending Images.
I used the following code.
public class Picture extends AppCompatActivity {
ImageView mparticularimage,
share;
BitmapDrawable drawable;
Bitmap bitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_picture);
getSupportActionBar().setTitle("Picture");
mparticularimage=findViewById(R.id.particularImage);
share=findViewById(R.id.share);
share.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//shareoption coding
shareImage();
}
});
private void shareImage() {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
drawable = (BitmapDrawable) mparticularimage.getDrawable();
bitmap = drawable.getBitmap();
File file = new File(getExternalCacheDir()+"/"+"Status downloaded by - Status saver"+".png");
Intent intent;
try {
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG,100, outputStream);
outputStream.flush();
outputStream.close();
intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/+");
intent.putExtra(intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}catch (Exception e){
throw new RuntimeException(e);
}
startActivity(Intent.createChooser(intent,"Share this image via: "));
}
}
The above code perfectly work for Images now how to do the same thing in terms of VideoView and .mp4 files.
Related
I am making a framer app which can be used to frame your image. And i Want to save that image in gallery after framing and for this i have set a button to perform this but in the button by below code i am unable to save my image. After clicking on button it crashes the app activity. please Someone Give me Solution. I am using api level >29.
``` dlbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
OutputStream outputStream;
BitmapDrawable drawable = (BitmapDrawable) mainimg.getDrawable();
Bitmap bitmap = drawable.getBitmap();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
{
ContentResolver resolver = MainActivity2.this.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME,"Image_"+".jpg");
contentValues.put(MediaStore.MediaColumns.MIME_TYPE,"image/jpeg");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH,Environment.DIRECTORY_PICTURES + File.separator+"TestFolder");
Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,contentValues);
try {
outputStream = resolver.openOutputStream(Objects.requireNonNull(imageUri) );
bitmap.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
Objects.requireNonNull(outputStream);
Toast.makeText(MainActivity2.this, "Image Saved", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity2.this, "Image Not Not Saved: \n "+e, Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
});
} ``
I stored url with id in firebase. I am using viewpager2 in xml. I want to share image or want to add share option in that. How can I do this.
public void onApplyImage(int position, Bitmap bitmap) {
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
try {
manager.setBitmap(bitmap);
Toast.makeText(SwiperActivity.this, "Wallpaper successfully set ", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(SwiperActivity.this, "Failed to set as wallpaper", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onShareImage(int position, Bitmap bitmap) {
}
});
Simple way to share is that first set you image to a ImageView and then use the following code to share you image.
ImageView content = (ImageView)mView.findViewById(R.id.imageViewy);
content.setDrawingCacheEnabled(true);
Uri imageUri= Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
content, "title", "discription"));
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
When I try to get the result from the camera I keep getting a "NullPointerException: uri" at the line:
InputStream stream = getContentResolver().openInputStream(data.getData());
from the code:
if (requestCode == CAMERA_PIC_REQUEST) {
try {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
pic.setImageBitmap(thumbnail);
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmapPic = BitmapFactory.decodeStream(stream);
Being pic an ImageViewer and the image is successfully placed in the ImageViewer I can't seem to find the reason as to why this is not working.
Also here's the code for the Intent that starts the image capturer.
pic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
});
And the part of the code that I will use it on after this is working, or at least trying to:
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmapPic.compress(Bitmap.CompressFormat.JPEG, 50, baos);
byte[] data = baos.toByteArray();
mStorageRef.child(animal.getPictureID()).putBytes(data);
Being the last line Firebase related.
Also bitmapPic is a Bitmap.
Edit 1: Tried a suggestion by adding global variable:
private FileProvider mImageUri;
then
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(Intent.EXTRA_STREAM, mImageUri);
startActivityForResult(intent, CAMERA_PIC_REQUEST);
}
But it says that putExtra combination doesn't exist. Not sure if that's what you meant.
Edit 2: Managed to get it working. As of now it only sends the Thumbnail but at least it's something here's how I did it:
if (requestCode == CAMERA_PIC_REQUEST) {
try {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
pic.setImageBitmap(thumbnail);
encodeBitmap(thumbnail);
}
where:
ByteArrayOutputStream baos;
public void encodeBitmap(Bitmap bitmap) {
baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
}
And finally to send it to firebase I used this:
byte[] data = baos.toByteArray();
mStorageRef.child(animal.getPictureID()).putBytes(data);
When I share animate gif to Twitter by using Intent, Twitter makes it static image.
It works to share to Facebook Messenger.
How to create an animated GIF from JPEGs in Android (development)
I use this class to create Animated gif.
Can anyone help me?
Here is my code below.
public byte[] generateGIF(ArrayList<Bitmap> bitmaps) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
AnimatedGifEncoder encoder = new AnimatedGifEncoder();
encoder.start(bos);
for (Bitmap bitmap : bitmaps) {
encoder.addFrame(bitmap);
}
encoder.finish();
return bos.toByteArray();
}
private void shareGif (byte[] bytes,String fileName) {
try {
File file = new File(getApplicationContext().getCacheDir(), fileName + ".gif");
FileOutputStream fOut = new FileOutputStream(file);
fOut.write(bytes);
fOut.close();
file.setReadable(true, false);
final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
intent.setType("image/gif");
startActivity(Intent.createChooser(intent , "Send image using.."));
} catch (Exception e) {
e.printStackTrace();
}
}
I noticed the default camera activity I call on a Droid X is different looking than the one on my Droid and Nexus One. After selecting "OK" on the Droid and Nexus One, the activity would finish - the Droid X has a "Done" button (which takes you back to the Camera, instead of finishing the activity), and the only way to get to the screen I want is to hit the "Back" button.
Here is the class that works on Android 2.2/2.3, but not for Droid X's:
package com.android.xxx;
import java.io.File;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Window;
public class CameraView extends MenusHolder {
protected String _path;
protected boolean _taken;
protected static final String PHOTO_TAKEN = "photo_taken";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.create_event_view);
/*
* save to sd
*/
File imageDirectory = new File(
Environment.getExternalStorageDirectory() + "/MyPath/");
imageDirectory.mkdirs();
/*
* temp image overwrites each time for space
*/
_path = Environment.getExternalStorageDirectory()
+ "/MyPath/temporary_image.jpg";
startCameraActivity();
}
protected void startCameraActivity() {
File file = new File(_path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case 0:
setResult(5);
finish();
break;
case -1:
onPhotoTaken();
break;
}
}
protected void onPhotoTaken() {
_taken = true;
setResult(0);
finish();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(CameraView.PHOTO_TAKEN, _taken);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(CameraView.PHOTO_TAKEN)) {
onPhotoTaken();
}
}
}
I solved this with a really really ugly workaround. I coded two functions to read and write files from sdcard (taken from here: http://www.sgoliver.net/blog/?p=2035).
private boolean readFile() {
try
{
File sd_path = Environment.getExternalStorageDirectory();
File f = new File(sd_path.getAbsolutePath(), "lock_camera_oncreate");
BufferedReader fin =
new BufferedReader(
new InputStreamReader(
new FileInputStream(f)));
String text = fin.readLine();
fin.close();
Log.e("Files", "Reading file");
return true;
}
catch (Exception ex)
{
Log.e("Files", "Error reading file from SD Card");
return false;
}
}
private void createFile() {
try
{
File sd_path = Environment.getExternalStorageDirectory();
File f = new File(sd_path.getAbsolutePath(), "lock_camera_oncreate");
OutputStreamWriter fout =
new OutputStreamWriter(
new FileOutputStream(f));
fout.write("Semaphore test.");
fout.close();
Log.e("Files", "File writed");
}
catch (Exception ex)
{
Log.e("Files", "Error reading file from SD Card");
}
}
Then, in onCreate function, I make this:
public void onCreate(Bundle savedInstanceState) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
if(readFile() == true)
{
File sd_path = Environment.getExternalStorageDirectory();
File f = new File(sd_path.getAbsolutePath(), "lock_camera_oncreate");
f.delete();
Intent intent = this.getIntent();
this.setResult(RESULT_OK, intent);
return;
}
createFile();
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCurrentImagePath)));
startActivityForResult(cameraIntent, TAKE_PHOTO_CODE);
}
The setRequestedOrientation call solves the issue when you are using your app in portrait mode, but when camera is launched, you put the mobile in landscape and then shoot the photo.
Then, the ugly readFile thing checks if a lock_camera_oncreate file exists and if it's true,
then an additional onCreate call happened, so delete file and RETURN from this activity.
If activity advances, means the file's not created and there is only one camera activity running.
Hope it helps, it's ugly but worked for me :D
Dude... it's just a bug. I had the same problem and there's no way to workaround that. It sometimes work, and sometimes it does not. I asked the Motorola's guy for help and they said that there's no support for those Android images.