Skip to content
Open

HW5 #11

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ android {
buildToolsVersion "23.0.2"

defaultConfig {
applicationId "com.csc.lesson5"
applicationId "ru.ppzh.filemanager"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
Expand All @@ -21,4 +21,5 @@ android {

dependencies {
compile 'com.android.support:appcompat-v7:23.2.1'
compile 'com.android.support:recyclerview-v7:23.2.1'
}
7 changes: 5 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.csc.lesson5">
package="ru.ppzh.filemanager">

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<activity android:name="ru.ppzh.filemanager.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="ru.ppzh.filemanager.ImageViewer"/>
</application>

</manifest>
12 changes: 0 additions & 12 deletions app/src/main/java/com/csc/lesson5/MainActivity.java

This file was deleted.

41 changes: 41 additions & 0 deletions app/src/main/java/ru/ppzh/filemanager/ImageLoader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ru.ppzh.filemanager;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class ImageLoader {

public Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) {

final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);

options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile(path, options);
}

public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {

final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 2;


while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}

return inSampleSize;
}
}
31 changes: 31 additions & 0 deletions app/src/main/java/ru/ppzh/filemanager/ImageViewer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package ru.ppzh.filemanager;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Display;
import android.widget.ImageView;

public class ImageViewer extends AppCompatActivity {
private ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image_viewer);
imageView = (ImageView) findViewById(R.id.image_viewer);

ImageLoader imageloader = new ImageLoader();
String imagePath = getIntent().getStringExtra(MainActivity.IMAGE_PATH);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Bitmap image = imageloader.decodeSampledBitmapFromFile(
imagePath,
size.x,
size.y
);
imageView.setImageBitmap(image);
}
}
97 changes: 97 additions & 0 deletions app/src/main/java/ru/ppzh/filemanager/Item.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package ru.ppzh.filemanager;


import android.graphics.Bitmap;
import android.support.annotation.NonNull;
import android.util.Log;

public class Item implements Comparable<Item> {
public static final String TAG = "Item";

private String name;
private String date;
private String path;
private Bitmap preview;
private boolean isDirectory;

public Item(String name, String date, String path, Bitmap preview, boolean isDirectory) {
this.name = name;
this.date = date;
this.path = path;
this.preview = preview;
this.isDirectory = isDirectory;
}

public void setName(String name) {
this.name = name;
}

public String getName() {
return name;
}

public void setDate(String date) {
this.date = date;
}

public String getDate() {
return date;
}

public void setPath(String path) {
this.path = path;
}

public String getPath() {
return path;
}

public Bitmap getPreview() {
return preview;
}

public boolean isDirectory() {
return isDirectory;
}

public int compareTo(@NonNull Item o) {
if (this.name != null)
return this.name.toLowerCase().compareTo(o.getName().toLowerCase());
else
throw new IllegalArgumentException();
}

public String getExtension() {
int index = name.lastIndexOf('.');
String extension = name.substring(index);

Log.i(TAG, extension);

return extension;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Item)) return false;

Item item = (Item) o;

if (isDirectory() != item.isDirectory()) return false;
if (!getName().equals(item.getName())) return false;
if (!getDate().equals(item.getDate())) return false;
if (!getPath().equals(item.getPath())) return false;
return getPreview().equals(item.getPreview());

}

@Override
public int hashCode() {
int result = getName().hashCode();
result = 31 * result + getDate().hashCode();
result = 31 * result + getPath().hashCode();
result = 31 * result + getPreview().hashCode();
result = 31 * result + (isDirectory() ? 1 : 0);
return result;
}
}
Loading