Button share bad url - java

i have problem when try share mp3 file from res/raw device throws an error "Invalid path"
My code:
Uri rawUri = getRawUri("sound1.mp3");
btn_shareaudio = (Button) findViewById(R.id.button);
btn_shareaudio.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("audio/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, rawUri);
startActivity(Intent.createChooser(shareIntent, "Condividi attraverso:"));
}
});
public Uri getRawUri(String filename) {
return Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + File.pathSeparator + File.separator + getPackageName() + "/raw/" + filename);
}

Related

Media Player does not play audio

I am developing an application that contains a RecyclerView with some audio players. The app will download the .3gp files if they have not been downloaded.
When I click the playAudio button, the audio is not being played.
Here is my adapter code:
else if( item.getTipo().equals(AUDIO)){
MediaPlayer mediaPlayer = new MediaPlayer();
File folder = new File(Environment.getExternalStorageDirectory() +
File.separator + "Audios");
File file = new File(folder.getPath() + File.separator
+ item.getNomeArquivo());
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference httpsReference = storage.getReferenceFromUrl( item.getAudio());
File localFile = null;
boolean success = true;
if (!folder.exists()) {
success = folder.mkdirs();
}
if (success) {
localFile = new File(folder.getPath() + File.separator
+ item.getNomeArquivo());
File finalLocalFile = localFile;
httpsReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(context,"Baixado",Toast.LENGTH_SHORT);
holder.progressAudio.setVisibility(View.GONE);
holder.playAudio.setVisibility(View.VISIBLE);
holder.playAudio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(mediaPlayer != null){
String path = Environment.getExternalStorageDirectory() +
File.separator + "Audios" + File.separator
+ item.getNomeArquivo();
Log.i("PATHA",path);
mediaPlayer.create(context,Uri.parse(path));
mediaPlayer.setVolume(50,50);
mediaPlayer.start();
}
}
});
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
Toast.makeText(context,"Erro ao fazer download",Toast.LENGTH_SHORT);
}
});
} else {
Toast.makeText(context, "Erro ao criar arquivo", Toast.LENGTH_SHORT).show();
}
}
How can I fix this?
Try replacing:
mediaPlayer.create(context,Uri.parse(path));
with:
mediaPlayer.create(context, Uri.fromFile(new File(path)));

How can I send video that I have recorded from one activity to another?

I am trying to send the video that I have recorded from one activity to another. I am thinking of sending the address but couldn't do so.
public void startRecordingVideo(View view) {
if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)) {
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
File mediaFile = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/myvideo.mp4");
videoUri = Uri.fromFile(mediaFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, videoUri);
startActivityForResult(intent, VIDEO_CAPTURE);
} else {
Toast.makeText(this, "No camera on device", Toast.LENGTH_LONG).show();
}
}
public void convert(View view){
Intent i = new Intent(getApplicationContext(),convertVid.class);
File mediaFile = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/myvideo.mp4");
videoUri = Uri.fromFile(mediaFile);
i.putExtra("video1",videoUri);
startActivityForResult(i,1);
}
}

Uri add text from edittext to link

findNumber.setOnClickListener(new View.OnClickListener() {
Uri uri = Uri.parse("http://wwww.whitepages.ca/phone/" + phoneNumber.getText());
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
#Override
public void onClick(View v) {
startActivity(intent);
}
});
When I do that it only opened up "http://www.whitepages.com/phone/"

Saving image from app / camera

I have this code in MainActivity:
onCreate() :
mtestImage = (ImageView) findViewById(R.id.testImage);
mtestImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
public void onActivityResult(int reqCode, int resCode, Intent data):
if (resCode == RESULT_OK) {
if (reqCode == 1) {
mtestImage.setImageURI(data.getData());
}
}
It works and ImageView shows me the picture that I opened, but if I restart Activity :
Intent intent = getIntent();
finish();
startActivity(intent);
The ImageView is restore default picture. How Can I save image that I took to work with it in future? (I'm trying to change BG of DrawerMenu with new pictures, that user can choose).
I founud some code in internet, but I'm not sure how I can combine it with my code:
private File createImageFile() throws IOException
{
String timeSnap = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeSnap + " ";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File image = File.createTempFile(imageFileName, ".jpg", storageDir);
return image;
}
THANK YOU!
Try this code.
private Uri getOutputMediaFile() {
File mediaStorageDir = new File(
Environment.getExternalStorageDirectory(), ".camerapics");
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
Uri uri = null;
if (mediaFile != null) {
uri = Uri.fromFile(mediaFile);
}
return uri;
}
mtestImage = (ImageView) findViewById(R.id.testImage);
mtestImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
fileUri = getOutputMediaFile();
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, RESULT_LOAD_IMAGE);
}
});
public void onActivityResult(int reqCode, int resCode, Intent data)
{
if (resCode == RESULT_OK) {
if (reqCode == RESULT_LOAD_IMAGE) {
Bitmap bmp = BitmapFactory.decodeStream(getContentResolver().openInputStream(fileUri));
mtestImage.setImageBitmap(bmp);
setUsername(this,fileUri.getPath());
}
}
}

Android open downloaded file

I have a question about downloading a pdf file and opening it with an pdf reader application installed on the phone. I'm new to android and working my way up but got stuck on this part.
So what i have now:
I have an activity that for now starts downloading a pdf file and tries to open is with an intent. For now everything is static so thats why i have a set url.
private void DownloadFile(){
DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse("http://awebiste.adomain/afile.pdf");
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle("My Data Download");
request.setDescription("Android Data download using DownloadManager.");
request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS,"test.pdf");
Long downloadReference = downloadManager.enqueue(request);
if (downloadReference != null){
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.parse(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/test.pdf"), "application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Log.v("OPEN_FILE_PATH", getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS) + "/test.pdf");
startActivity(target);
} else {
//TODO something went wrong error
}
}
Now the file is downloaded and saved in the application folder under /storage.sdcard0/Android/data/<application>/files/download and from the documents browser the file can be opent. But when i use the intent at the button of my code i get a toast that the file can not be opent. After some searching on google i think its a permission problem because these files are private to the application. So how do I make these files public?
Here's How I did it after a long day of search,Your code helped me a little to solve it:
String name;
DownloadManager mManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.show_interview);
Button down = (Button) findViewById(R.id.download);
down.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
down();
}
});
}
public void down() {
mManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
Uri downloadUri = Uri.parse(DOWNLOAD_FILE);
DownloadManager.Request request = new DownloadManager.Request(
downloadUri)
.setAllowedOverRoaming(false)
.setTitle("Downloading")
.setDestinationInExternalPublicDir(
Environment.DIRECTORY_DOWNLOADS, name + "CV.pdf")
.setDescription("Download in progress").setMimeType("pdf");
}
#Override
protected void onResume() {
super.onResume();
IntentFilter intentFilter = new IntentFilter(
DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(broadcast, intentFilter);
}
public void showPdf() {
try {
File file = new File(Environment.getExternalStorageDirectory()
+ "/Download/" + name + "CV.pdf");//name here is the name of any string you want to pass to the method
if (!file.isDirectory())
file.mkdir();
Intent testIntent = new Intent("com.adobe.reader");
testIntent.setType("application/pdf");
testIntent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
testIntent.setDataAndType(uri, "application/pdf");
startActivity(testIntent);
} catch (Exception e) {
e.printStackTrace();
}
}
BroadcastReceiver broadcast = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
showPdf();
}
};

Categories

Resources