2016-03-31 6 views
2

Я работаю над приложением, в котором я хочу открыть любую фотографию в своем приложении из внешнего проводника файлов. Например, когда я нажимаю на фотографию, появляется окно приложений, и я выбираю свое приложение, а изображение отображается в моем приложении. Но здесь изображение не появляется.Как открыть одно изображение в моем приложении из файла explorer

Я добавил код в Manifest.xml:

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.image.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
      <intent-filter> 
       <action android:name="android.intent.action.SEND" /> 
       <action android:name="android.intent.action.VIEW" /> 
       <action android:name="android.intent.action.EDIT" /> 

       <category android:name="android.intent.category.BROWSABLE" /> 
       <category android:name="android.intent.category.DEFAULT" /> 

       <data android:mimeType="image/*" /> 
       <data android:mimeType="text/*" /> 
      </intent-filter> 
     </activity> 
</application> 

Компоновка файл activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="10dp" 
    tools:context=".DataReceiverActivity" > 

    <ImageView 
     android:id="@+id/picture" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:contentDescription="Receive Picture Sharing" /> 

    <TextView 
     android:id="@+id/txt" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" /> 

</RelativeLayout> 

И в моем MainActivity.java:

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     ImageView picView = (ImageView) findViewById(R.id.picture); 
     TextView txtView = (TextView) findViewById(R.id.txt); 

     Intent receivedIntent = getIntent(); 
     String receivedAction = receivedIntent.getAction(); 
     String receivedType = receivedIntent.getType(); 

     // make sure it's an action and type we can handle 
     if (receivedAction.equals(Intent.ACTION_SEND)) { 
      if (receivedType.startsWith("text/")) { 
       picView.setVisibility(View.GONE); 
       String receivedText = receivedIntent 
        .getStringExtra(Intent.EXTRA_TEXT); 
       if (receivedText != null) { 
        txtView.setText(receivedText); 
       } 
      } else if (receivedType.startsWith("image/")) { 
       txtView.setVisibility(View.GONE); 
       Uri receivedUri = (Uri) receivedIntent 
        .getParcelableExtra(Intent.EXTRA_STREAM); 
       if (receivedUri != null) { 
        picView.setImageURI(receivedUri);// just for demonstration 
       } 
      } 
     } else if (receivedAction.equals(Intent.ACTION_VIEW)) { 

      if (receivedType.startsWith("text/")) { 
       Toast.makeText(this, "TextRecived", Toast.LENGTH_SHORT).show(); 
       Uri uri2 = receivedIntent.getData(); 
       String uri = uri2.getEncodedPath() + " complete: " 
        + uri2.toString(); 
       txtView.setText(uri); 
      } else if (receivedType.startsWith("image/")) { 

       txtView.setVisibility(View.GONE); 
       Uri receivedUri = (Uri) receivedIntent 
        .getParcelableExtra(Intent.EXTRA_STREAM); 
       Toast.makeText(this, receivedUri.toString(), Toast.LENGTH_SHORT).show(); 
       if (receivedUri != null) { 
        picView.setImageURI(receivedUri);// just for demonstration 
       } 
      } 
     } else if (receivedAction.equals(Intent.ACTION_MAIN)) { 
      txtView.setText("-----------------------------"); 
     } 
    } 

} 

Вот мои скриншоты:

opening image from explorer when selecting my app empty screen appare

+0

Ну, где это происходит? Вы должны предоставить информацию об отладке. – greenapps

+0

'изображение показано в моем приложении. Но здесь изображения не отображается.' ???? Если это шоу, то оказалось, что я подумаю. Где ты говоришь? – greenapps

+0

Пожалуйста, введите код. Вероятно, вы должны начать с getIntent или около того. – greenapps

ответ

1

Проблема в том, что вы пытаетесь получить Uri неправильным образом. Изменение всех мест где

Uri receivedUri = (Uri) receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM); 

в

Uri receivedUri = receivedIntent.getData(); 

это поможет вам правильно Uri файла. Также стоит обратить внимание, что setImageURI делает

чтение и декодирование Bitmap в потоке пользовательского интерфейса

См this ссылку для получения более подробной информации.

+0

Да, я уже пытаюсь, как это видеть мой код ... никак, как получить изображение, это не работает – Attaullah

+0

Нет, у вас нет. Блок кода if (receivedType.startsWith ("image /")) вы все еще используете метод getParcelableExtra –

+0

спасибо, но я имею в виду, что я уже пробовал это также ... – Attaullah