Skip to content

Commit

Permalink
Add camera upload
Browse files Browse the repository at this point in the history
  • Loading branch information
briceln committed Nov 19, 2018
1 parent 8eae51b commit 0e87abf
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 19 deletions.
4 changes: 2 additions & 2 deletions .idea/assetWizardSettings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion app/src/main/java/brice_bastien/epicture/DialogUpload.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class DialogUpload extends DialogFragment {
public MyPostsRecyclerViewAdapter adapter;
public ImgurApi imgurApi;


EditText title;
EditText description;
public Uri img;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ public void uploadImg(Uri img, String title, String desc) {
new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
Log.w("Upload", response.toString());
Log.w("UploadResponse", response.toString());
Toast.makeText(context, context.getText(R.string.upload_success), Toast.LENGTH_SHORT).show();
}
}, new ErrorListener());
Expand All @@ -283,7 +283,8 @@ public void onResponse(NetworkResponse response) {
request.setRetryPolicy(new DefaultRetryPolicy(2500, 0, 1.0f));
requestQueue.add(request);
} catch (Exception e) {
Log.w("Upload:", "failed");
e.printStackTrace();
Log.w("Upload", e.toString());
}
}

Expand Down
87 changes: 75 additions & 12 deletions app/src/main/java/brice_bastien/epicture/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,39 @@
package brice_bastien.epicture;

import android.Manifest;
import android.app.Dialog;
import android.app.FragmentManager;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.SearchView;
import android.widget.Toast;

import com.google.android.material.bottomappbar.BottomAppBar;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import brice_bastien.epicture.ImgurApi.ImgurApi;
import brice_bastien.epicture.post.PostItem;
Expand All @@ -36,10 +50,12 @@ public class MainActivity extends AppCompatActivity implements PostsFragment.OnL
private BottomAppBar bar;
private FragmentManager fragmentManager = getFragmentManager();
private ImgurApi imgurApi;
private static final int REQUEST_CODE = 42;
private static final int REQUEST_CODE_CAMERA = 24;
private static final int REQUEST_CODE_FILE_EXPLORER = 42;
String[] PERMISSIONS = {
android.Manifest.permission.WRITE_EXTERNAL_STORAGE,
android.Manifest.permission.READ_EXTERNAL_STORAGE
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CAMERA
};

@Override
Expand Down Expand Up @@ -74,18 +90,49 @@ protected void onCreate(Bundle savedInstanceState) {
postsFragment = PostsFragment.newInstance(2, imgurApi);
fragmentManager.beginTransaction().replace(R.id.include, postsFragment).commit();
fab = findViewById(R.id.fab);
final AppCompatActivity main = this;
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE);
final Dialog dialog = new Dialog(main);
dialog.setTitle(R.string.modal_upload_choose);
dialog.setCancelable(true);
dialog.setContentView(R.layout.dialog_image);
dialog.show();

Button camera = dialog.findViewById(R.id.camera_action);
Button fileExplorer = dialog.findViewById(R.id.file_explorer_action);

camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(main, Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePicture.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePicture, REQUEST_CODE_CAMERA);
}
} else {
ActivityCompat.requestPermissions(main, PERMISSIONS, 1);
}
dialog.dismiss();
}
});

fileExplorer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, REQUEST_CODE_FILE_EXPLORER);
dialog.dismiss();
}
});

}
});

imgurApi.getRecentImg(postsFragment, "hot");
// Snackbar.make(this.getVie, "", Snackbar.LENGTH_SHORT).show();
}

@Override
Expand Down Expand Up @@ -161,9 +208,7 @@ public boolean onOptionsItemSelected(MenuItem item) {
break;
case (R.id.app_bar_search):
break;

case (R.id.action_sort):
//showEditDialog();
showEditDialog();
break;
case (android.R.id.home):
Expand All @@ -188,13 +233,24 @@ public void onListFragmentInteraction(PostItem item) {

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE:
case REQUEST_CODE_FILE_EXPLORER:
if (data != null) {
imagePath = data.getData();
upload_dialog = true;
}
break;
case REQUEST_CODE_CAMERA:
if (data != null) {
Bitmap img = (Bitmap)data.getExtras().get("data");
if (img != null) {
imagePath = getImageUri(getApplicationContext(), img);
upload_dialog = true;
}
}
break;
}
}
}
Expand All @@ -209,6 +265,13 @@ protected void onResumeFragments() {
}
}

private Uri getImageUri(Context context, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(context.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}

private void showUploadDialog(Uri img) {
DialogUpload editNameDialogFragment = DialogUpload.newInstance(postsFragment.adapter, img, imgurApi);
editNameDialogFragment.show(getSupportFragmentManager(), "fragment_upload_picture");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/res/drawable/ic_camera.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M12,12m-3.2,0a3.2,3.2 0,1 1,6.4 0a3.2,3.2 0,1 1,-6.4 0"/>
<path
android:fillColor="#FF000000"
android:pathData="M9,2L7.17,4L4,4c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h16c1.1,0 2,-0.9 2,-2L22,6c0,-1.1 -0.9,-2 -2,-2h-3.17L15,2L9,2zM12,17c-2.76,0 -5,-2.24 -5,-5s2.24,-5 5,-5 5,2.24 5,5 -2.24,5 -5,5z"/>
</vector>
24 changes: 24 additions & 0 deletions app/src/main/res/layout/dialog_image.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button
android:id="@+id/camera_action"
app:icon="@drawable/ic_camera"
android:text="@string/camera_action"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

<Button
android:id="@+id/file_explorer_action"
app:icon="@drawable/ic_photo_lib_white"
android:text="@string/file_explorer_action"
style="@style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</LinearLayout>
3 changes: 3 additions & 0 deletions app/src/main/res/values-fr/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,8 @@
<string name="grid_layout">Mode grille</string>
<string name="grid_layout_hint">Active le mode grille dans le flux. Redémarrer après changement !</string>
<string name="hello">Bonjour %1$s !</string>
<string name="camera_action">Prendre une photo</string>
<string name="file_explorer_action">Choisir une image dans le télephone</string>
<string name="modal_upload_choose">Prendre une photo ou un fichier</string>

</resources>
7 changes: 5 additions & 2 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@
<string name="dark_mode_hint">Enable Dark Mode. Note: not fully implement</string>
<string name="commentary_sort">Recent Comments</string>
<string name="commentary_sort_hint">If enabled, the comments will be sorted by the most recent</string>

<!-- Modal upload -->

<string name="modal_upload_title">Title</string>
Expand All @@ -79,6 +79,9 @@
<string name="modal_upload_description_hint">Description</string>
<string name="modal_upload_button">Upload</string>

<string name="modal_upload_choose">Choose an action</string>
<string name="camera_action">Take a picture</string>
<string name="file_explorer_action">Take a picture in gallery</string>

<string name="action_favorite">Favorite</string>
<string name="action_search">Search</string>
Expand Down Expand Up @@ -121,7 +124,7 @@
</plurals>

<plurals name="numberOfComments">
<item quantity="one">See the %d comment</item>
<item quantity="one">See all reviews</item>
<item quantity="other">See all %d reviews</item>
</plurals>

Expand Down

0 comments on commit 0e87abf

Please sign in to comment.