Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/PhotoPlugin/PhotoActivity.java
- This file was added.
1 | package org.kde.kdeconnect.Plugins.PhotoPlugin; | ||||
---|---|---|---|---|---|
2 | | ||||
3 | import android.content.Intent; | ||||
4 | import android.net.Uri; | ||||
5 | import android.os.Environment; | ||||
6 | import android.provider.MediaStore; | ||||
7 | | ||||
8 | import org.kde.kdeconnect.BackgroundService; | ||||
9 | | ||||
10 | import java.io.File; | ||||
11 | import java.io.IOException; | ||||
12 | import java.text.SimpleDateFormat; | ||||
13 | import java.util.Date; | ||||
14 | | ||||
15 | import androidx.annotation.Nullable; | ||||
16 | import androidx.appcompat.app.AppCompatActivity; | ||||
17 | import androidx.core.content.FileProvider; | ||||
18 | | ||||
19 | public class PhotoActivity extends AppCompatActivity { | ||||
20 | | ||||
21 | private Uri photoURI; | ||||
22 | private PhotoPlugin plugin; | ||||
23 | | ||||
24 | @Override | ||||
25 | protected void onStart() { | ||||
26 | super.onStart(); | ||||
27 | | ||||
28 | BackgroundService.runWithPlugin(this, getIntent().getStringExtra("deviceId"), PhotoPlugin.class, plugin -> { | ||||
29 | this.plugin = plugin; | ||||
30 | }); | ||||
31 | | ||||
32 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | ||||
33 | if (takePictureIntent.resolveActivity(getPackageManager()) != null) { | ||||
34 | File photoFile = null; | ||||
35 | try { | ||||
36 | photoFile = createImageFile(); | ||||
37 | } catch (IOException ignored) { | ||||
38 | } | ||||
39 | // Continue only if the File was successfully created | ||||
40 | if (photoFile != null) { | ||||
41 | photoURI = FileProvider.getUriForFile(this, | ||||
42 | "org.kde.kdeconnect_tp.fileprovider", | ||||
43 | photoFile); | ||||
44 | takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); | ||||
45 | startActivityForResult(takePictureIntent, 1); | ||||
46 | } | ||||
47 | } | ||||
48 | | ||||
49 | } | ||||
50 | | ||||
51 | private File createImageFile() throws IOException { | ||||
52 | // Create an image file name | ||||
53 | String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()); | ||||
54 | String imageFileName = "JPEG_" + timeStamp + "_"; | ||||
55 | File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES); | ||||
56 | return File.createTempFile( | ||||
57 | imageFileName, /* prefix */ | ||||
58 | ".jpg", /* suffix */ | ||||
59 | storageDir /* directory */ | ||||
60 | ); | ||||
61 | } | ||||
62 | | ||||
63 | @Override | ||||
64 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | ||||
65 | super.onActivityResult(requestCode, resultCode, data); | ||||
66 | | ||||
67 | if (resultCode == -1) { | ||||
68 | plugin.sendPhoto(photoURI); | ||||
69 | } | ||||
70 | finish(); | ||||
71 | } | ||||
72 | } |