i want to share my apllication's apk to another device, and this is not working.
this code is from the internet.. how can i fix this and share my app from my app
private void shareApplication() {
ApplicationInfo app = getApplicationContext().getApplicationInfo();
String filePath = app.sourceDir;
Intent intent = new Intent(Intent.ACTION_SEND);
// MIME of .apk is "application/vnd.android.package-archive".
// but Bluetooth does not accept this. Let's use "*/*" instead.
intent.setType("*/*");
// Append file and send Intent
File originalApk = new File(filePath);
try {
//Make new directory in new location
File tempFile = new File(getExternalCacheDir() + "/ExtractedApk");
//If directory doesn't exists create new
if (!tempFile.isDirectory())
if (!tempFile.mkdirs())
return;
//Get application's name and convert to lowercase
tempFile = new File(tempFile.getPath() + "/" + getString(app.labelRes).replace(" ","").toLowerCase() + ".apk");
//If file doesn't exists create new
if (!tempFile.exists()) {
if (!tempFile.createNewFile()) {
return;
}
}
//Copy file to new location
InputStream in = new FileInputStream(originalApk);
OutputStream out = new FileOutputStream(tempFile);
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
System.out.println("File copied.");
//Open share dialog
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(tempFile));
startActivity(Intent.createChooser(intent, "Share app via"));
} catch (IOException e) {
e.printStackTrace();
}
}
it`s send the apk in same weight as the generating of android atudio but when install the app crash.
Related
how to prevent storing same file selected in galary twice in internal storage in android .I tried with below code it copies same video many times in a folder in the internal storage .
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
new SaveVideoInFolder().execute(uri);
try {
InputStream is = getContentResolver().openInputStream(uri);
File storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
File app_directory = new File(storage, "video_choosing");
if (!app_directory.exists())
app_directory.mkdirs();
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String filename = String.format("VID_%s.mp4", timestamp);
file = new File(app_directory, filename);
Toast.makeText(MainActivity.this,file.toString(),Toast.LENGTH_SHORT).show();
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int read;
while ((read = is.read(buffer)) != -1)
output.write(buffer, 0, read);
output.flush();
output.close();
} catch (FileNotFoundException e) {
Log.e("TAG", "File Not Found", e);
} catch (IOException e) {
Log.e("TAG", "IOException", e);
}
}
// Create the storage directory if it does not exist
if (!file.exists()) {
if (!file.mkdirs()) {
/* Log.e(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
+ IMAGE_DIRECTORY_NAME + " directory");*/
return null;
}
only you have check that your file is exist bt if condition and make directory if it is not..
File file = new File(app_directory, filename);
if(file.exists()){
...
}
else {
...
}
I'm working on an application that allows the user to import videos and edit them. The applications allows the user to import local videos, or videos from Google Photos or Drive. The following code was working
Intent i = new Intent();
i.setType("video/mp4");
i.setAction(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
startActivityForResult(i, REQUEST_CODE);
and then, on the onActivityResult method, I copy to file from Photos or Drive
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
try {
File storage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);
File app_directory = new File(storage, APP_NAME);
if (!app_directory.exists())
app_directory.mkdirs();
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
String filename = String.format("VID_%s.mp4", timestamp);
File file = new File(app_directory, filename);
InputStream is = getContentResolver().openInputStream(uri);
OutputStream output = new FileOutputStream(file);
byte[] buffer = new byte[4096];
int read;
while ((read = is.read(buffer)) != -1)
output.write(buffer, 0, read);
output.flush();
output.close();
} catch (FileNotFoundException e) {
Toast.makeText(this, "File Not Found", Toast.LENGTH_LONG).show();
Log.e(TAG, "File Not Found", e);
} catch (IOException e) {
Toast.makeText(this, "Could not save the file", Toast.LENGTH_LONG).show();
Log.e(TAG, "IOException", e);
}
}
However since the latest update of the Google Photos app I'm only getting very small file that I cannot open with any application, and sometimes for oldest videos I'm getting a FileNotFoundException
I have tried a couple of solutions, like changing the intent to request the file like this
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.setType("video/mp4");
startActivityForResult(i, REQUEST_CODE);
or getting the InputStream through a FileDescriptor
ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(uri, "r");
FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
InputStream is = new FileInputStream(fileDescriptor);
With the same result
Hello in my application i need to backUp my Database file to google Drive account via the Intent class, i just create the file and send it on "Intent.ACTION_SEND" and the user need to choose Google drive . now, i want to import it from the google drive to my app again.that is possible?
and one more thing.. there is a way to upload it directly to google drive ? because the intent chooser give the user all the options.
here is my code how i save it:
#SuppressLint("SdCardPath")
private void exportDB() throws IOException {
// Open your local db as the input stream
try {
String inFileName = "/data/data/com.appsa.work/databases/"+DbHandler.DB_NAME;
File dbFile = new File(inFileName);
FileInputStream fis = new FileInputStream(dbFile);
String outFileName = Environment.getExternalStorageDirectory() + "/work/MyDatabase";
// Open the empty db as the output stream
OutputStream output = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = fis.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
// Close the streams
output.flush();
output.close();
fis.close();
} catch (Exception e) {
Toast.makeText(context, e.toString(), Toast.LENGTH_LONG)
.show();
}
Toast.makeText(context,
"Uploaded.",
Toast.LENGTH_LONG).show();
}
public void sendDb(String mailAddres){
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] {mailAddres});
intent.putExtra(Intent.EXTRA_SUBJECT, "my file");
File root = Environment.getExternalStorageDirectory();
File file = new File(root, "/work/MyDatabase");
if (!file.exists() || !file.canRead()) {
Toast.makeText(context, "no sd_card", Toast.LENGTH_SHORT).show();
return;
}
Uri uri = Uri.parse("file://" + file.getAbsolutePath());
intent.putExtra(Intent.EXTRA_STREAM, uri);
context.startActivity(Intent.createChooser(intent, "Send"));
}
You can do this using the Drive Android API. See creating files and working with file contents.
I am attaching an image from drawable folder and sending it in a email.
when I send it from default email client.Image extension(.png) is missing in attachment
and and also the file name is changed itself.
I want t send image with default name(as in drawable) and with .png extension.
this is my code.
Intent email = new Intent(Intent.ACTION_SEND);
email.setType("image/png");
email.putExtra(Intent.EXTRA_EMAIL, new String[] { to });
email.putExtra(Intent.EXTRA_SUBJECT, "Hey! ");
email.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://"+ getPackageName() + "/" + R.drawable.ic_launcher));
startActivity(Intent.createChooser(email, "Sending........"));
Please suggest me what is worng in this code
thanks.
You can only attach an image to mail if the image is located in the SDCARD.
So, you'll need to copy the image to SD and then attach it.
InputStream in = null;
OutputStream out = null;
try
{
in = getResources().openRawResource(R.raw.ic_launcher);
out = new FileOutputStream(new File(Environment.getExternalStorageDirectory(), "image.png"));
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
}
catch (Exception e)
{
Log.e("tag", e.getMessage());
e.printStackTrace();
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/html");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "File attached");
Uri uri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), "image.png"));
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
i am trying to share an image from the drawables in my application. i keep getting java.io.FileNotFoundException:/abc.jpg (read-only file system)
my code is
private String SaveCache(int resID) {
String path = "";
try {
InputStream is = getResources().openRawResource(resID);
File cacheDir = this.getExternalCacheDir();
File downloadingMediaFile = new File(cacheDir, "abc.jpg");
byte[] buf = new byte[256];
FileOutputStream out = new FileOutputStream(downloadingMediaFile);
while (true) {
int rd = is.read(buf, 0, 256);
if (rd == -1 || rd == 0)
break;
out.write(buf, 0, rd);
}
is.close();
out.close();
return downloadingMediaFile.getPath();
} catch (Exception ex) {
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
ex.printStackTrace();
}
return path;
}
private void ImageShare() {
String path = SaveCache(R.drawable.testsend);
Toast.makeText(this, path, Toast.LENGTH_LONG).show();
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_SUBJECT, "test");
share.putExtra(Intent.EXTRA_TEXT, "test");
share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + path));
try {
startActivity(Intent.createChooser(share, "Choose share method."));
} catch (Exception ex) {
ex.printStackTrace();
}
}
Any help is appreciated, if more info is required i will be happy to provide.
Make sure that the external storage is mounted.
From the docs of getExternalCacheDir():
Returns the path of the directory holding application cache files on
external storage. Returns null if external storage is not currently
mounted so it could not ensure the path exists; you will need to call
this method again when it is available.
http://www.anddev.org/post3469.html#p3469
You need to use
FileOutputStream fos = openFileOutput(name, MODE);
as mentioned in the example.