I have a feature in my app which allows the user to download a new version.
But my problem is when I start the intent which should start the installation proccess there is a message:
Parse error. There was a problem parsing the package
This is my code
Button button;
String urlApp = "example.com/app.apk"
String fileName = "app.apk";
DownloadManager downloadManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
File file = new File(Environment.DIRECTORY_DOWNLOADS, fileName);
if (file.exists()){
boolean deleted = file.delete();
Toast.makeText(getApplicationContext(), "Deleted", Toast.LENGTH_SHORT).show();
}
downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlApp));
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setTitle("Click to update");
downloadManager.enqueue(request);
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
});
}
private void openFile() {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS+"/"+fileName)),
"application/vnd.android.package-archive");
startActivity(install);
}
BroadcastReceiver onComplete = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
openFile();
}
};
}
It's just for .apk files, .txt files work fine. There is neither a error on the logcat.
The deleting is also not working but this isn't my primary problem.
Thanks in advance :D
Sorry if this is a stupid problem but this is my first time working with the download manager
Ok, i finally solved it
Instead of:
install.setDataAndType(Uri.fromFile(new File(Environment.DIRECTORY_DOWNLOADS+"/"+fileName)),
"application/vnd.android.package-archive");
i used
install.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/"+fileName)),
"application/vnd.android.package-archive");
I have come across the same problem earlier.
Please check the Min API level for your new Application version and the API level of the device you are using.
Related
I am recording a video and passing its path into VideoView Activity. But the video can't be opened. Can anyone please help?
RecordActivity - video path function-:
public static String getVideoFilePath() {
return getAndroidMoviesFolder().getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + "rangeela.mp4";
}
Its returning ->
file:///storage/emulated/0/DCIM/202209_02-231740rangeela.mp4
File mediaFile = new File(getVideoFilePath());
Uri uri= Uri.fromFile(mediaFile);
Intent intent=new Intent(this,VideoEditorActivity.class);
intent.putExtra("uri",uri.toString());
startActivity(intent);
VideoViewActivity
Bundle bundle=getIntent().getExtras();
if(bundle!=null){
Uri uri=Uri.parse(bundle.getString("uri"));
Log.e("video path = ", String.valueOf(uri));
binding.videoView.setVideoURI(uri);
binding.videoView.start();
}
But I am getting an error
Can't play this video
How to pass the recorded video in intent and play in another activity?
Try this
String path = getAndroidMoviesFolder().getAbsolutePath() + "/" + new SimpleDateFormat("yyyyMM_dd-HHmmss").format(new Date()) + "rangeela.mp4";
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(this,VideoEditorActivity.class);
intent.putExtra("video",path);
}
});
VideoViewActivity
VideoView view = findViewById(R.id.videoview);
String path = getIntent().getStringExtra("video");
view.setVideoPath(path);
view.start();
I'm trying to set up a share button on my app. The button is supposed to take a screenshot of a particular list view, and then allow the user to share this image via whatever means they want. To do this, I created three methods:
Take the screenshot
Save it in my storage
Share it to the user.
To do this I've written the following code:
public class ViewPlayerHistoryContents extends AppCompatActivity {
public static File imagePath;
public Bitmap takeScreenshot() {
View rootView = findViewById(android.R.id.content).getRootView();
rootView.setDrawingCacheEnabled(true);
return rootView.getDrawingCache();
}
public void saveBitmap(Bitmap bitmap) {
imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
} catch (FileNotFoundException e) {
Log.e("GREC", e.getMessage(), e);
} catch (IOException e) {
Log.e("GREC", e.getMessage(), e);
}
}
private void shareIt() {
Uri uri = Uri.fromFile(imagePath);
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("image/*");
String shareBody = "In Tweecher, My highest score with screen shot";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Tweecher score");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view_player_history);
shareButton = findViewById(R.id.shareButton);
View rootView = getWindow().getDecorView().findViewById(android.R.id.content);
shareButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Bitmap bitmap = takeScreenshot();
saveBitmap(bitmap);
shareIt();
}
});
}
}
With this code I keep getting the following error message:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.transfergame, PID: 18477
android.os.FileUriExposedException: file:///storage/emulated/0/screenshot.png exposed beyond app through ClipData.Item.getUri()
Which to me sounds like it's because I'm using Uri and not FileProvider?
How should I be incorporating FileProvider in this?
I added the following permissions to my Android manifest file, but it hasn't done anything:
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
How can I fix this error?
Thanks everyone
You can go through this
You have add provider in android manifest and also create the a File Provider in xml/provider.xml as the answer in thus link suggests.
public void captureImage() {
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
activity.startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
I have button for for install apk file download from server in specific folder and user some time delete apk file from folder but this button do not disable in same time but should exit activity and open again ... my question is how refresh status button after delete file apk by user without open activity again
Button install = (Button) findViewById(R.id.install_font);
final File file_1 = new File(Environment.getExternalStorageDirectory() + "/download/app-debug.apk");
if(file_1.exists()){
install.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/"+"app-debug.apk")),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
}else {
install_font.setEnabled(false);
}
}
and i try add onResume but disable button all time mean if apk file exists the button is disable and apk file not exists the button is disable
#Override
public void onResume() {
super.onResume();
final File file = new File(Environment.getExternalStorageDirectory() + "/download/app-debug.apk");
final Button install_font = (Button) findViewById(R.id.install_font);
install_font.setEnabled(false);
}
You could check if your file exist every time the user enter to the application , then set the enabled flag based on it.
Try this code:
#Override
public void onResume() {
super.onResume();
final File file = new File(Environment.getExternalStorageDirectory() + "/download/app-debug.apk");
final Button install_font = (Button) findViewById(R.id.install_font);
install_font.setEnabled(file.exists());
}
I have a webview app and wanted to updated app from inside when a certain text is present in the url.
I am calling these in shouldOverrideUrlLoading function of webview:
Intent intent = new Intent(getApplicationContext(), Update.class);
startActivity(intent);
return true;
And here is the Update.class
public class Update extends MainActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
String destination = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/";
String fileName = "app-file.apk";
destination += fileName;
final Uri uri = Uri.parse("file://" + destination);
File file = new File(destination);
if (file.exists())
file.delete();
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(getIntent().getStringExtra("http://example.com/app-file.apk")));
request.setDestinationUri(uri);
dm.enqueue(request);
final String finalDestination = destination;
final BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri contentUri = FileProvider.getUriForFile(ctxt, BuildConfig.APPLICATION_ID + ".provider", new File(finalDestination));
Intent openFileIntent = new Intent(Intent.ACTION_VIEW);
openFileIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
openFileIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
openFileIntent.setData(contentUri);
startActivity(openFileIntent);
unregisterReceiver(this);
finish();
} else {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(install);
unregisterReceiver(this);
finish();
}
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
}
When I go to the certain url to start the activity, the app crashes. I would love to see any solution. Thanks!!
I SOLVED IT, it was permission error.
I would suggest you to look into ADB Logcat, which will help you to fix the issue.
Otherwise, check into crash log or make sure that you have declared your Update Activity in manifest.xml
Make sure you declare the new activity in your Android Manifest.xml
try changing this
Intent intent = new Intent(getApplicationContext(), Update.class);
startActivity(intent);
return true;
to this
Intent intent = new Intent(getApplicationContext(), Update.class);
startActivity(intent);
finish();
return true;
Let me know how that went.
So I've been practising download manager and i was trying out some program.
A first button will download a said Pdf to a location in my phone.
The second button is supposed to open the Pdf file with a Pdf reader installed by the user such as WP Reader etc..
The download works fine, but when i open the pdf , it says invalid format.
I uploaded a sample Pdf to the google drive , so I know that the uploaded file isnt corrupted in any case. There muse be some problem when the file is being downloaded from the server. Please help me find the mistake. And I am relatively new to Android.
And the download button uses an Onclicklistener while the loadPdf is given in the xml file android:onClick="downloadPdf" .
public class MainActivity extends AppCompatActivity {
String myHTTPUrl = "https://drive.google.com/open?id=0B5Pev9zz5bVjZTFFZ1dLZVp1WVU";
String TAG = "My app";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button download = (Button) findViewById(R.id.download);
download.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.v(TAG,"download method called");
downloadPdf();
Toast.makeText(MainActivity.this,"Listener Called",Toast.LENGTH_SHORT).show();
Log.v(TAG,"On Click Listener Called");
}
});
}
public void loadPdf(View view) {
Log.v(TAG,"Pdf load called");
File pdfFile = new File(Environment.getExternalStorageDirectory()+"/notes","ssp.pdf");
if(pdfFile.exists())
{
Uri path = Uri.fromFile(pdfFile);
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(path, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = Intent.createChooser(pdfIntent, "Open File");
try
{
startActivity(intent);
}
catch(ActivityNotFoundException e)
{
Toast.makeText(MainActivity.this, "No Application available to view pdf", Toast.LENGTH_LONG).show();
}
}
else
{
Toast.makeText(MainActivity.this,"Pdf not found",Toast.LENGTH_SHORT).show();
}
}
public void downloadPdf()
{
DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(myHTTPUrl));
request.setTitle("Solid State Physics");
request.setDescription("File is being Downloaded...");
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir("/notes","ssp.pdf");
manager.enqueue(request);
}}
EDITED :
This is the exact error I'm facing with screenshot
Error : File format error , Cannot be opened
Screenshot of the error.
You're not downloading the PDF, you're downloading the web-page displaying the contents of the PDF. If you want to download the PDF file directly, use the following URL:
String myHTTPUrl = "https://drive.google.com/uc?export=download&id=0B5Pev9zz5bVjZTFFZ1dLZVp1WVU";
and your application should work.
(Don't forget to delete the invalid notes/ssp.pdf file first, otherwise the DownloadManager will download the file under a different name, like notes/ssp-1.pdf.)