从Android中的特定文件名加载图像

时间:2018-08-29 10:16:10

标签: android

我创建了一个应用程序,用于将屏幕截图保存到USB存储设备。但是我无法从USB存储器打开该图像。请帮我解决这个问题。我不想打开该图像并将其保留在ImageView中,但是我想加载图像查看器或其他类似我们从Gallery中打开的应用程序。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/main"
tools:context=".MainActivity">

<Button
    android:id="@+id/btn"
    android:text="Take Screen"
    android:layout_centerHorizontal="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ImageView
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="250dp"
    android:layout_below="@+id/btn"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="30dp"
    android:background="@color/colorPrimary"
    android:scaleType="fitCenter" />

<Button
    android:id="@+id/btnLoad"
    android:text="Take Screen"
    android:layout_below="@+id/imageView"
    android:layout_centerHorizontal="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

  </RelativeLayout>
  1. MainActivity.java

    btn_load.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
            // My image name "restaurant.jpg"
            // image keep in USB storage\restaurant.jpg
    
        }
    });
    

2 个答案:

答案 0 :(得分:0)

Glide是一个开放源代码库,可以显示任何来源的图像。您可以在以下步骤中使用它。

添加以下文件: build.gradle

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.8.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}

MainActivity

//Initialize ImageView
ImageView imageView = (ImageView) findViewById(R.id.imageView);

btn_load.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
      //Loading image from any file location into imageView
     String photoPath = Environment.getExternalStorageDirectory()+"/restaurent.jpg"; // need to add storage read permission
     Glide.with(MainActivity.this)
          .load(new File(photoPath))
          .into(imageView);
    }
});

希望这会有所帮助。

答案 1 :(得分:0)

//现在这是如何从存储或外部应用程序中打开PDF文件

// Open from Storage File
public void openFileFromStorage(){

    File file = new File("/sdcard/testpdf/test.pdf");
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(Uri.fromFile(file),"application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
    startActivity(intent);
}

// Open External App. How to find Package name install "System Info" in Play Store.
public void openAppExternal(){
    Intent intent = getPackageManager().getLaunchIntentForPackage("com.adobe.reader");
    startActivity(intent);
}

// Open External Website
public static Intent openApp(Context context){
    try {
        context.getPackageManager().getPackageInfo("https://www.geeksforgeeks.org/file-class-in-java/",0);
        return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.geeksforgeeks.org/file-class-in-java/"));

    }catch (Exception e){
        return new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.geeksforgeeks.org/file-class-in-java/"));
    }
}
相关问题